我使用AlertDialog使用xml文件显示一些数据。但是当我在AlertDialog中膨胀xml时,默认按钮不会显示。 请帮忙
答案 0 :(得分:1)
只需在xml文件中添加新按钮,然后获取该按钮的ID,并将click监听器放在此按钮上。 这对我有用。
答案 1 :(得分:0)
LayoutInflater factory = LayoutInflater.from(Splash.this);
final View textEntryView = factory.inflate(R.layout.text_entry, null);
//text_entry is an Layout XML file containing two text field to display in alert dialog
final EditText input1 = (EditText) textEntryView.findViewById(R.id.editText1);
final EditText input2 = (EditText) textEntryView.findViewById(R.id.editText2);
input1.setHint("roomnumber");
input2.setHint("hotelname");
AlertDialog.Builder alert = new AlertDialog.Builder(Splash.this);
alert.setIcon(android.R.drawable.ic_menu_info_details)
.setTitle("Enter the Text:")
.setView(textEntryView)
.setPositiveButton("Your Default button text",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Log.i("AlertDialog","TextEntry 1 Entered "+input1.getText().toString());
Log.i("AlertDialog","TextEntry 2 Entered "+input2.getText().toString());
if(isOnline())
{
/* User clicked OK so do some stuff */
}else{
}
}
});
alert.show();
答案 2 :(得分:0)
这将解决您的问题! 您需要将onclick侦听器设置为对话框中的所有按钮。请遵循以下代码。
public void showDialogWithIcons() {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.icons_dialog);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
Button yesButton = (Button) dialog.findViewById(R.id.yesanswer_icons);
yesButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
TextView tvForIconToChange = (TextView) findViewById(R.id.textInThePicture);
tvForIconToChange.setTypeface(tf);
tvForIconToChange.setText(getMyTextForIcon());
dialog.dismiss();
}
});
dialog.show();
}
答案 3 :(得分:0)
因为它的自定义布局。 要在自定义对话框中使用按钮,请在ur自定义布局xml中定义按钮,然后在代码中执行以下操作:
Button xyz = (Button)dialog.findViewbyId(R.id.abc)
xyz.setOnClickListener(this or new View.OnClickListener)