2011-10-17 47 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