单击AlertDialog中的正向按钮后,如何禁用按钮5到10秒。
e.g。当我点击片段中的按钮时会弹出一个AlertDialog然后当我点击OK按钮时,片段中的按钮会被禁用一段时间。
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" />
答案 0 :(得分:1)
试试这个,这将禁用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();
答案 1 :(得分:1)
解决问题(对于FELLOW COMRADE)
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 寻求帮助