当我向 alertDialog 添加布局按钮时。它不显示任何内容,它强制停止应用程序。如何为 AlertDialog 中的按钮设置 onClickListener。我想在按钮按下时显示一个时间选择器
LayoutInflater li = LayoutInflater.from(this);
final View promptsView = li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
startTimeButton=(Button)promptsView.findViewById(R.id.buttonStartTime);
stopTimeButton=(Button)promptsView.findViewById(R.id.buttonStopTime);
startTimeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "btn clocled", Toast.LENGTH_LONG).show();
// showDialog(TIME_DIALOG_ID);
}
});
stopTimeButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "btn clicled", Toast.LENGTH_LONG).show();
// showDialog(TIME_DIALOG_ID);
}
});
alertDialogBuilder.setCancelable(false).setPositiveButton("OK",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
final RadioGroup radioGroup = (RadioGroup)promptsView.findViewById(R.id.radioGroup1);
int checkedRadioButton = 0;
try {
checkedRadioButton = radioGroup.getCheckedRadioButtonId();
} catch (Exception e) {
e.printStackTrace();
}
int i=0;
toggleLogging(AppSettings.getServiceRunning(MainActivity.this),
AppSettings.setLoggingInterval(MainActivity.this,currentIntervalChoice));
dialog.dismiss();
// return;
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
like this:
<Button
android:id="@+id/buttonStartTime"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Set Start Time" />
<TextView
android:id="@+id/StartTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="startTime"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="@+id/buttonStopTime"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Set Stop Time" />