2015-10-04 130 views
2

如果用户单击按钮,会出现一个对话框,要求输入一个字符串,并且在同一个对话框中有一个“确定”按钮,当用户按下该按钮时,对话框应该关闭。这至少是计划,问题是:将事件处理程序添加到“确定”按钮后,当用户打开对话框时,我的应用程序会冻结。关闭对话框,当按下确定按钮时

addNewFamButton = FindViewById<Button>(Resource.Id.newFamilyButton); 
addNewFamButton.Click += (sender, e) => { 
    Dialog dialog = new Dialog(this); 
    dialog.SetContentView(Resource.Layout.addNewFamily); 
    dialog.SetTitle("Add new family to the list"); 
    dialog.Show(); 

    // Problem starts here: 
    Button saveNewFamily = FindViewById<Button>(Resource.Id.dialogButtonOK); 
    saveNewFamily.Click += (object o, EventArgs ea) => { dialog.Dispose(); };     
}; 

我试着用dialog.Cancel(),但我得到了相同的结果。如果我删除了最后两行,那么对话框可以正常工作,但显然不会关闭。

固定:感谢user370305了简单的解决方案:

Button saveNewFamily = dialog.FindViewById<Button>(Resource.Id.dialogButtonOK); 

回答

2

OK按钮Dialog视图的一部分,所以你必须使用你的对话对象的引用,类似的发现来看, (我不熟悉xamarin但是这一个给你提示)

更改线路,

// Problem starts here: 
Button saveNewFamily = FindViewById<Button>(Resource.Id.dialogButtonOK); 

Button saveNewFamily = dialog.FindViewById<Button>(Resource.Id.dialogButtonOK); 
+0

这是我的尴尬问题的解决方案。 – hungariandude

2

试试这个

 // create an EditText for the dialog 
     final EditText enteredText = new EditText(this); 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Title of the dialog"); 
     builder.setView(enteredText); 
     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() 
     { 
      @Override 
      public void onClick(DialogInterface dialog, int id) 
      { 
       // perform any operation you want 
       enteredText.getText().toString());// get the text 

       // other operations 
       dialog.cancel(); // close the dialog 

      } 
     }); 
     builder.create().show(); 
相关问题