![](/img/trans.png)
[英]How to know which radiobutton was clicked when ok button is clicked in alertdialog?
[英]Disable a button in the fragment/xml file when “ok” is clicked on the alertdialog
单击AlertDialog中的肯定按钮后,如何在5到10秒钟内禁用按钮。
例如,当我单击片段中的按钮时,将弹出AlertDialog,然后当我单击确定按钮时,片段中的按钮将被禁用一段时间。
public class LockDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Title");
builder.setMessage("LOCK");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//HERE WHEN I CLICK OK THE BUTTON IN THE XML FILE WILL BE DISABLED
Toast.makeText(getActivity(), "LOCK", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return builder.create();
}
}
XML:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/btnStart"
android:layout_below="@+id/space"
android:layout_centerHorizontal="true"
android:background="#125688"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold"
android:onClick="onStart" />
尝试此操作,这将禁用具有id按钮1的按钮5秒钟
final AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setTitle("Test");
final EditText input = new EditText(MainActivity.this);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),
"Disabled" + input.getText().toString(),
Toast.LENGTH_SHORT).show();
findViewById(R.id.button1).setEnabled(false);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Enabled" + input.getText().toString(),
Toast.LENGTH_SHORT).show();
findViewById(R.id.button1).setEnabled(true);
}
}, 5000);
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// This should I want.?!
dialog.cancel();
}
});
alertDialog = builder.show();
解决问题(同志)
public Dialog onCreateDialog(Bundle savedInstanceState) {
final ImageButton aw = (ImageButton)getActivity().findViewById(R.id.btnLock);
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Lock");
builder.setMessage("MESSAGE");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "LOCKING", Toast.LENGTH_SHORT).show();
aw.setEnabled(false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
aw.setEnabled(true);
}
},5000);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return builder.create();
}
}
TY Hitesh Sahu的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.