簡體   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