[英]How To create a Dialog from clicking a button on a custom dialog?
For part of the app I am making. 对于我正在制作的应用程序的一部分。 I have a button being selected in an options menu. 我在选项菜单中选择了一个按钮。 When that button is clicked, It opens a custom dialog. 单击该按钮时,它将打开一个自定义对话框。 I need buttons on that custom dialog to open a new Dialog, but for some reason my methodology causes the app to crash. 我需要在该自定义对话框上打开新对话框的按钮,但是由于某种原因,我的方法导致应用崩溃。 Any assistance would be much appreciated. 任何帮助将不胜感激。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_app);
//Copy and paste this line to onClick methods regarding the edit text dialog
// textentry = (EditText)findViewById(R.id.entrybox);
// Button year = (Button)findViewById(R.id.Year);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options, menu);
return true;
}
//Dialog Box for selecting View books in Options Menu
public Dialog ViewBookOptions() {
AlertDialog.Builder builder =
new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View v = inflater.inflate(R.layout.view_books, null);
builder.setView(v);
final AlertDialog dialog = builder.create();
return dialog;
}
//Dialog boxes for selecting Year, Author, or Title in the ViewBook Dialog Box
public Dialog ViewByYear(){
AlertDialog.Builder builder =
new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View v = inflater.inflate(R.layout.text_entry, null);
builder.setView(v);
final EditText entrybox = (EditText) v.findViewById(R.id.entrybox);
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String entry = entrybox.getText().toString().toLowerCase();
//This is Where You Will Populate The ListView With Database Stuff
}
});
builder.setNegativeButton(
"Cancel",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
return dialog;
}
//Quick Toast for checking stuff
public void toast(String string){
Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
}
//All of my button OnClick Methods
public void allbooks(View view){
toast("You clicked All books");
}
public void year(View view){
toast("Dialog should open and EditText should change hint text");
EditText textentry = (EditText)findViewById(R.id.entrybox);
textentry.setHint("Enter the Year of the Book(s)");
Dialog dialog = ViewByYear();
dialog.show();
}
// The Options Menu
@Override
public boolean
onOptionsItemSelected
(MenuItem item) {
switch (item.getItemId()) {
case R.id.option1:
//Change to View Book Options
Dialog dialog = ViewBookOptions();
dialog.show();
return true;
case R.id.option2:
return true;
case R.id.option3:
return true;
case R.id.option4:
return true;
case R.id.option5:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
That is my activity code. 那是我的活动代码。 I believe the error has something to do with the first dialog not being dismissed before the second dialog is created, but I am unsure how to correct this, because my dialogs are called by different buttons at different instances. 我相信该错误与创建第二个对话框之前未关闭第一个对话框有关,但是我不确定如何更正此错误,因为我的对话框在不同实例中由不同的按钮调用。
Why don't you try out Sweet Alert Dialog Android Library ? 您为什么不尝试使用Sweet Alert Dialog Android库 ? It supports the use of nested dialogs in code. 它支持在代码中使用嵌套对话框。 Here's the link: https://github.com/pedant/sweet-alert-dialog 这是链接: https : //github.com/pedant/sweet-alert-dialog
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.