Android:AlertDialog未显示
我希望在活动中按下时显示一个警报对话框(从用户处获取一些输入的表单),例如Android:AlertDialog未显示,android,android-layout,android-alertdialog,Android,Android Layout,Android Alertdialog,我希望在活动中按下时显示一个警报对话框(从用户处获取一些输入的表单),例如 @Override public void onBackPressed() { showFormDialog(); } private void showFormDialog() { //Preparing views LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
@Override
public void onBackPressed() {
showFormDialog();
}
private void showFormDialog() {
//Preparing views
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dialog_form, (ViewGroup) findViewById(R.id.llid));
//layout_root should be the name of the "top-level" layout node in the dialog_layout.xml file.
final EditText txtFB = (EditText) layout.findViewById(R.id.txtFB);
//Building dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//save info where you want it
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
}
dialog\u form.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/llid"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/txtFB"
android:layout_width="250dp"
android:layout_height="200dp"
android:text="hello type something here"/>
</LinearLayout> </LinearLayout>
我可以看到showFormDialog代码是在反按时执行的,但没有显示AlertDialog 您错过了呼叫
.show()
.show()
使用提供给此生成器的参数创建一个AlertDialog,并立即显示对话框。只需添加此参数即可
dialog.show();
仅仅创造它是不够的。您应该显示它。添加对话框。show()
:
哎呀。。。错过了!
dialog.show();
AlertDialog dialog = builder.create();
dialog.show();