2011-03-29 55 views
0

如何使用AlertDialog同步獲取文本?舉例來說,我會想要做這樣的事情:使用AlertDialog同步獲取文本

public String GetTextDialog() 
    {   
    final EditText text = new EditText(activity); 

    // Gets the chat 
    final AlertDialog.Builder dialog = new AlertDialog.Builder(activity); 
    dialog.setView(text); 
    dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface arg0, int arg1) { 
     } 
    }); 
    dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface arg0, int arg1) { 
     } 
    }); 

    // Display 
    dialog.show(); 

    // Return text after dialog is complete 
    return text.getText().toString(); 
    } 

編輯:你會認爲這是一個更好的辦法:

interface TextHandler 
{ 
    public String Title(); 
    public void HandleText(String text); 
} 

public static boolean ShowTextDialog(
     String title, 
     String defaultValue, 
     final TextHandler posButton, 
     final TextHandler negButton, 
     final TextHandler neutButton, 
     int width 
     ) 
{ 
    // Check for already existing dialog 
    if(showDialog) return false; 
    showDialog = true; 

    // Verify context 
    if(context == null) 
    { 
     Log.e("Crystal", "Screen::ShowTextDialog No Context!"); 
     showDialog = false; 
     return false; 
    } 

    // Verify we have buttons 
    if(neutButton == null) 
    { 
     if(posButton == null || negButton == null) 
     { 
      Log.e("Crystal", "Screen::ShowTextDialog Must supply both postive and negative buttons!"); 
      showDialog = false; 
      return false; 
     } 
    } 
    else 
    { 
     if(posButton != null || negButton != null) 
     { 
      Log.e("Crystal", "Screen::ShowTextDialog Can't have neutral button with other type!"); 
      showDialog = false; 
      return false; 
     } 
    } 

    // Create the dialog 
    AlertDialog.Builder dialog = new AlertDialog.Builder(context); 
    if(title != null) dialog.setTitle(title); 

    // Create the chat 
    final EditText text = new EditText(context); 
    text.setSingleLine(); 
    if(defaultValue != null) text.setText(defaultValue); 
    if(width != 0) text.setWidth((int)(width*context.getResources().getDisplayMetrics().density)); 

    // Add text to dialog 
    dialog.setView(text); 

    if(posButton != null) 
    { 
     dialog.setPositiveButton(posButton.Title(), new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface arg0, int arg1) { 
       posButton.HandleText(text.getText().toString()); 
       showDialog = false; 
      } 
     }); 
    } 

    if(negButton != null) 
    { 
     dialog.setNegativeButton(negButton.Title(), new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface arg0, int arg1) { 
       negButton.HandleText(text.getText().toString()); 
       showDialog = false; 
      } 
     }); 
    } 

    if(neutButton != null) 
    { 
     dialog.setNeutralButton(neutButton.Title(), new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface arg0, int arg1) { 
       neutButton.HandleText(text.getText().toString()); 
       showDialog = false; 
      } 
     }); 
    } 

    // Display 
    dialog.show(); 

    return true; 
} 

而且這樣調用:

private void GetUsernameDialog() 
{ 
    ShowTextDialog(
      "Enter Username",             // Title 
      username,               // Default value 
      new TextHandler()             // Positive button 
      { 
       public String Title() { return "Ok"; } 
       public void HandleText(String text) { SetUsername(text); } 
      }, 
      new TextHandler()             // Negative button 
      { 
       public String Title() { return "Cancel"; } 
       public void HandleText(String text) { } 
      }, 
      null,                // Neutral button 
      200                 // Width 
     ); 
} 

回答

2

你可以在其中有一個Looper的替代線程中運行該對話框,在對話解除時終止循環,讓主UI線程等待另一個線程......但是這個是如此複雜的設計,如果你這樣做,上帝可能會殺死一隻小貓。請問爲什麼你需要同步數據訪問?這只是爲了方便,還是你有設計理由?

如果是從對話框設置之前訪問狀態,那麼嵌套的匿名類就是你的答案。上面的代碼片段中已經有了這些代碼。他們可以自動訪問嵌套函數的變量以及嵌套類的成員。

+0

我想要一個函數,可以返回從用戶檢索到的文本。如果有更好的東西,我不需要使用AlertDialog。 – 2011-03-29 04:28:35

+0

GUI本質上是異步的(事件驅動的)。另外,與Windows不同,Android無法在消息循環中運行消息循環。我明白你想要什麼;問題是 - 爲什麼? – 2011-03-29 04:32:39

+0

我想我可能重新設計我的代碼是異步的,但我希望有一個簡單的函數來調用能夠獲取文本。如果沒有簡單的方法做到這一點,我可以嘗試另一種方式。 – 2011-03-29 04:37:17