繁体   English   中英

如何通过单击自定义对话框上的按钮来创建对话框?

[英]How To create a Dialog from clicking a button on a custom dialog?

对于我正在制作的应用程序的一部分。 我在选项菜单中选择了一个按钮。 单击该按钮时,它将打开一个自定义对话框。 我需要在该自定义对话框上打开新对话框的按钮,但是由于某种原因,我的方法导致应用崩溃。 任何帮助将不胜感激。

@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);
          }
       }

那是我的活动代码。 我相信该错误与创建第二个对话框之前未关闭第一个对话框有关,但是我不确定如何更正此错误,因为我的对话框在不同实例中由不同的按钮调用。

您为什么不尝试使用Sweet Alert Dialog Android库 它支持在代码中使用嵌套对话框。 这是链接: https : //github.com/pedant/sweet-alert-dialog

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM