如何在同一活动中处理多个对话框

时间:2013-02-15 11:10:01

标签: android layout dialog alert

我正在创建一个应用程序,该活动需要3个以上的对话框用于不同目的。我正在使用onCreateDialog()方法,该方法当前正在处理XML文件中定义的单个对话框,我需要知道的是如何在不使代码变大的情况下处理更多xml文件。

@Override
protected Dialog onCreateDialog(int id ){

        return createExampleDialog(); // Function call, body is defined below
}

// This method is used for setting the dialogbox 
@Override
protected void onPrepareDialog(int id,Dialog loginPrompt){
    final EditText first = (EditText)loginPrompt.findViewById(R.id.rf1);
    first.setText(""); // setting the editbox blank
    final EditText second = (EditText)loginPrompt.findViewById(R.id.rf2);
    second.setText("");
}

//Body the function createExampleDialog() 
private Dialog createExampleDialog(){
    LayoutInflater factory = LayoutInflater.from(this);
    final View textEntryView = factory.inflate(R.layout.userpasslayout, null);
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Add Location!");
    alert.setIcon(R.drawable.logo);
    alert.setMessage("Fill information carefully!");
    alert.setView(textEntryView);
    //AlertDialog loginPrompt = alert.create();

    //PositiveButton Onclick Listener
    alert.setPositiveButton("Register", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Toast.makeText(DialogActivity.this, "So you think!", Toast.LENGTH_LONG).show(); 
            return;
        }
    });

    //NegativeButton onclick Listener
    alert.setNegativeButton("cancel", new DialogInterface.OnClickListener() {       
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            // NegtiveButton works automatic it will perform cancel of the dialog box;

            return;
        }
    }); 
    return alert.create();
}   

}

1 个答案:

答案 0 :(得分:1)

使用

    switch(id){
         case exampleDialog_id:
             return showExapleDialog();
         break;
         case secondDialog_id:
             returnshowSecondDialog;
         break;
         default:
         return null;}