2011-10-17 19 views
0

在活动“A”中,我重写了onCreateDialog以创建一个EditText对话框 在活动“A”的onCreate中,我调用一个方法调用showDialog,然后我派生另一个活动B使用意图。Android对话框不等待TextInput和OK点击

我希望用户在EditText中输入一些内容,然后按Ok,然后应该启动Intent !!。, 但是会发生什么情况,弹出对话框,新的活动被Intent 。

基本上,代码不会对OnClickListener等待对话框。它是因为DialogBox不会停止主UI线程?为什么它是这样的,我需要做些什么来获得这种行为,等待用户在对话框中输入内容,然后按确定继续。

代码模板

@Override 
protected Dialog onCreateDialog(int id) { 
    switch(id) {    
    case DIALOG_TEXT_ENTRY: 

     LayoutInflater factory = LayoutInflater.from(this); 
     final View textEntryView = factory.inflate(R.layout.alert_dialog, null); 

     return new AlertDialog.Builder(ExampleActivityy.this) 
      .setTitle(R.string.name_title_string) 
      .setView(textEntryView) 
      .setPositiveButton(R.string.ok_string, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 

        /* User clicked OK so do some stuff */ 
       } 
      }) 
      .setNegativeButton(R.string.cancel_string, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 

        /* User clicked cancel so do some stuff */ 
       } 
      }) 
      .create(); 

      .../... 
      .../... 
      ...// further down inside onCreate 
@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    this.mTitleRightImageButton.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      Intent spawnEmptyIntent = new Intent(Intent.ACTION_INSERT, getIntent().getData()); 
      showDialog(DIALOG_TEXT_ENTRY); /// UI does not wait here 
      onCreateNewList(); // and proceed further to this section 
      Log.w(TAG, "Intent action string is " + getIntent().getDataString()); 
      startActivity(spawnEmptyListIntent); // and launches this activity. 
     } 

回答

0

试试这个,

.setPositiveButton(R.string.ok_string, new DialogInterface.OnClickListener() 
{     
    public void onClick(DialogInterface dialog, int whichButton) 
    {      
    // User clicked OK so do some stuff 
    Intent spawnEmptyIntent = new Intent(Intent.ACTION_INSERT, getIntent().getData()); 

    startActivity(spawnEmptyListIntent); 

    }    
}) 

this.mTitleRightImageButton.setOnClickListener(new OnClickListener() {   
    public void onClick(View v) 
    { 
    showDialog(DIALOG_TEXT_ENTRY); /// UI does not wait here 
    onCreateNewList(); // and proceed further to this section    
    Log.w(TAG, "Intent action string is " + getIntent().getDataString()); 
    } 
0

你应该把代码启动新的活动在相关onClickListener

+0

感谢肯定会尝试, – devgp