0

我设计了一个alertdialog,当用户按下save按钮时,希望它能够将用户在alert dialog中输入的editTexts(球员的分数)返回给activity,以便进一步处理。

这怎么能做成?会有样品吗?非常感谢!!

我已经执行了以下操作,但现在的输出是它无法识别正在输入的内容并要求用户输入非空白的 editText(通过底部的 else 路由):

public OnClickListener NewRowButtonListener = new OnClickListener()  
   {  
      @Override  
      public void onClick(View v)   
      {       
           AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);  

           dialog.setTitle("Add Scores"); //set title  
           View view = getLayoutInflater().inflate(R.layout.custom_dialog_add, null);  
           dialog.setView(view); 
           dialog.create();             
           final AlertDialog test = dialog.create();            
           SlotAlert_add_EditText = (EditText) view.findViewById(R.id.Slot_Alert_add_EditText); 
           P1Alert_add_ScoreEditText = (EditText) view.findViewById(R.id.P1_Alert_add_ScoreEditText); 
           P2Alert_add_ScoreEditText = (EditText) view.findViewById(R.id.P2_Alert_add_ScoreEditText); 
           P3Alert_add_ScoreEditText = (EditText) view.findViewById(R.id.P3_Alert_add_ScoreEditText); 
           P4Alert_add_ScoreEditText = (EditText) view.findViewById(R.id.P4_Alert_add_ScoreEditText);           

           Button alert_add_save = (Button) view.findViewById(R.id.Alert_add_SaveButton);  //find the Button object within the inflated view                                    
           alert_add_save.setOnClickListener(addButtonListener);        //set the onClickListener    
           test.show();     //show the dialog  
      } 
   }; 

进而

public OnClickListener addButtonListener = new OnClickListener() 
   // create a new Button and add it to the ScrollView    
   { 
      private String SlotAppend0EditText; 
      @Override 
      public void onClick(View v)  
      {            
            SlotEditText.setText(""+SlotAlert_add_EditText); //try to set EditText in MainActivity using the one in the alert dialog above 
            P1ScoreEditText.setText(""+P1Alert_add_ScoreEditText);  
            P2ScoreEditText.setText(""+P2Alert_add_ScoreEditText); 
            P3ScoreEditText.setText(""+P3Alert_add_ScoreEditText);  
            P4ScoreEditText.setText(""+P4Alert_add_ScoreEditText); 

         if (SlotEditText.getText().length() <4 && 
             SlotEditText.getText().length() > 0 && 
             P1ScoreEditText.getText().length() > 0 &&  
             P2ScoreEditText.getText().length() > 0 && 
             P3ScoreEditText.getText().length() > 0 &&  
             P4ScoreEditText.getText().length() > 0 )             
         { 
            SaveScoresToFile(SlotAppend0EditText, 
                    P1ScoreEditText.getText().toString(),  
                    P2ScoreEditText.getText().toString(),  
                    P3ScoreEditText.getText().toString(),  
                    P4ScoreEditText.getText().toString()); 
                         } // end if 
         else  (another alert message saying please enter valid input) //NOW THE RESULT IS SHOWING THIS! 
4

1 回答 1

0

看到这个

 Button b = (Button) findViewById(R.id.LeftMenu);
    b.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
             CreateAlertDialog ();
        }
    });

警报对话框

 private void CreateAlertDialog () {         

    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());

    alert.setTitle("Title");
    alert.setMessage("Message");

    // Set an EditText view to get user input 
    final EditText input = new EditText(getActivity());
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

        }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

        }
    });
    alert.show();       
}
于 2012-10-02T09:55:26.293 回答