Android: AlertDialog 未显示
Android: AlertDialog not showing up
我希望在 Activity 中按回时显示一个警告对话框(一种从用户那里获取一些输入的表单),例如
@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_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()
dialog.show();
.show()
使用提供给该构建器的参数创建一个 AlertDialog 并立即显示对话框。
只需添加这个
dialog.show();
创建它是不够的。你应该展示它。
添加dialog.show()
:
AlertDialog dialog = builder.create();
dialog.show();
我希望在 Activity 中按回时显示一个警告对话框(一种从用户那里获取一些输入的表单),例如
@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_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()
dialog.show();
.show()
使用提供给该构建器的参数创建一个 AlertDialog 并立即显示对话框。
只需添加这个
dialog.show();
创建它是不够的。你应该展示它。
添加dialog.show()
:
AlertDialog dialog = builder.create();
dialog.show();