我有一个执行一些验证的对话框(如下)。问题是,在显示Toast之后对话框被解除,而我没有调用dismiss。我需要显示吐司并保持对话框打开以纠正错误。
final EditText txtName = new EditText(this);
AlertDialog.Builder dlgAdd = new AlertDialog.Builder(this)
.setTitle(R.string.create_category)
.setMessage(R.string.name)
.setView(txtName)
.setPositiveButton(R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String newCatName = txtName.getText().toString().trim(); // Converts the value of getText to a string.
if (newCatName != null && newCatName .length() ==0)
{
Toast.makeText(ManageCategories.this, R.string.err_name_required, 3500).show();
} else {
try {
boolean alreadyExists = mDatabaseAdapter.getCategoryIDs(newCatName).length > 0;// ids of cats with this name
if(alreadyExists) {
Toast.makeText(ManageCategories.this, R.string.categoryAlreadyExists, 3500).show();
} else {
mDatabaseAdapter.addCategory(newCatName);
}
}catch(Exception ex){
Toast.makeText(ManageCategories.this, R.string.error+':'+ ex.getLocalizedMessage(), 3500).show();
}
}
}
}).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
dlgAdd.show();
答案 0 :(得分:1)
我的猜测是你没有使用OnCreateDialog函数创建和显示Android文档http://developer.android.com/guide/topics/ui/dialogs.html中提到的对话框
请按照文档中的说明进行操作,如果仍然无效,请告知我们。
答案 1 :(得分:1)
我认为使用AlertDialog.bilder无法实现您想要实现的任何目标 而不是你可以做
实施例
dialog_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<EditText
android:layout_height="wrap_content"
android:id="@+id/EditText01" android:layout_width="300dip" android:ellipsize="none"/>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/Button01"
android:layout_height="wrap_content"
android:text="Yes"
android:layout_width="100dip"/>
<Button
android:id="@+id/Button02"
android:layout_height="wrap_content"
android:text="No"
android:layout_width="100dip"/>
</LinearLayout>
</LinearLayout>
Help.java
public class Help extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
d = new Dialog(Help.this,
android.R.style.Theme_InputMethod);
createMyDialog();
}
private Dialog d;
private void createMyDialog() {
d.setContentView(R.layout.dialog_view);
Button b1 = (Button)findViewById(R.id.Button01);
Button b2 = (Button)findViewById(R.id.Button02);
EditText t = (EditText) findViewById(R.id.EditText01);
OnTouchListener listner1 = null;
OnTouchListener listner2 = null;
b1.setOnTouchListener(listner1);
b2.setOnTouchListener(listner2);
listner1 = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
};
listner2 = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
};
d.show();
}
}