问题
I am new in android programming. I searched on the internet but i can't find any solution, maybe for this i have not good english. anyway. When user clicked on the web link i want to show an alarm dialog to user that turn on wifi, and then user clicked on the OK button, wifi settings is shows.
This is my TextView with link:
<TextView
android:id="@+id/sitename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/site2"
android:background="@color/white"
android:layout_below="@id/address"
android:layout_toLeftOf="@id/site"
android:paddingTop="3dp"
android:paddingRight="5dp"
android:autoLink="web"/>
and i set text of web with strings.xml.
sorry if my question was duplicated and sorry for my poor english. Cheers.
EDIT: Contact.java:
public class Contact extends Fragment {
public String fonts="Tahoma.TTF";
Typeface face;
TextView siteName;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.contact, container, false);
siteName=(TextView)view.findViewById(R.id.sitename);
siteName.setOnClickListener(onClickListener);
return view;
}
private OnClickListener onClickListener=new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.sitename:
AlertDialog.Builder alertDialog=new AlertDialog.Builder(getActivity());
alertDialog.setTitle("Turn on WI-Fi..");
alertDialog.setMessage("Do you want to go to wifi settings?");
alertDialog.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));
}
});
alertDialog.show();
break;
}
}
};
}
回答1:
Try this on button click:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(
this);
// Setting Dialog Title
alertDialog.setTitle("Confirm...");
// Setting Dialog Message
alertDialog.setMessage("Do you want to go to wifi settings?");
// Setting Icon to Dialog
// alertDialog.setIcon(R.drawable.ic_launcher);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Activity transfer to wifi settings
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("no",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
回答2:
This should work
startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));
回答3:
Try adding the following to the onClick
of Button
context.startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));
This will redirect the user directly to the wifi settings screen
Hope this works
来源:https://stackoverflow.com/questions/18811694/alarm-dialog-for-turn-on-wifi