2010-08-17 59 views
0

我一直在試圖獲得對話框的句柄。我閱讀了Android開發站點的信息和一打bolg's。看起來有幾種方法可以做到這一點,我至少可以通過3個按鈕進行對話(不使用自定義佈局的自定義對話框)。Android對話框 - 困惑

如果我用finish(),cancel()等類似的東西來設置正面/負面/中立的動作,它可以處理這些調用。

但是,我想要做的是讓按鈕做更多的事情,如果只使用主代碼中定義的字符串(而不是吐司)顯示文本。最後,我想在對話框中輸入一些數字並將它們以字符串形式返回。是的,我可以從另一個活動屏幕上做到這一點,但不要因爲我喜歡對話框的緊湊尺寸。

即使作弊和返回一個整數到Maincode做一些開關/案件的東西會沒事的,但是,我似乎甚至不能夠返回一個整數。

據我所知,我需要做一個自定義的警報對話框來做輸入的東西,以下是我試圖返回一個字符串或整數的嘗試 - 它似乎並沒有讓我在那裏!

此代碼顯示帶有3個按鈕的對話框。這只是我所做的一個嘗試(整數返回的東西刪除)...

我能做些什麼來從對話框的按鈕返回一個整數到Maincode?沒有代碼錯誤或警告,它就像我希望不工作...

public class Maincode extends Activity { 
public static String rtndMessage = "Push Button"; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    final TextView text = (TextView) findViewById(
      R.id.TextView01); 
    final Button doIt = (Button) findViewById(R.id.Button01); 

    // ----------------------------------------------------- 
    doIt.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // do something 
      Dialog1();   // call Dialog1 
     } 
    }); // end ----------------------------------------------- 
// do the rest of the code (ie, display the result of doIt) 
text.setText(rtndMessage); // set text w/result 
}//end onCreate ---------------------------------------------- 

public void Dialog1() { 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setTitle("Dialog Test"); 
builder.setIcon(R.drawable.redtest); 
// chain the following together 
    builder.setMessage("Send text: Yes, No, Cancel") 
    // the positive action ---------------------------------- 
    .setPositiveButton("Yes Action", new 
     DialogInterface.OnClickListener(){ 
     public void onClick(DialogInterface dialog, int id){    
      Maincode.rtndMessage = "sent Yes back";    
     } 
    }) 
    // The negative action ---------------------------------- 
    .setNegativeButton("No Action", new 
      DialogInterface.OnClickListener(){ 
     public void onClick(DialogInterface dialog, int id){ 
      Maincode.rtndMessage = "sent No back"; 
     } 
    }) 
    // The negative action ------------------------------ 
    .setNeutralButton("Cancel", new 
      DialogInterface.OnClickListener(){ 
     public void onClick(DialogInterface dialog, int id){ 
      Maincode.rtndMessage = "sent N/A back"; 
      dialog.cancel(); // just return to activity 
     }   
});  

builder.show(); //顯示對話框 } //結束對話框 } //結束活動

+0

您應該編輯您的問題,以便代碼正確顯示(確保每行前有4個空格)。那麼它會更容易幫助你:) – stealthcopter 2010-08-17 21:35:58

回答

4

您可以將對話框實現爲單獨的活動並獲得您需要的任何行爲。只需將標準android Theme.Dialog主題應用於該活動,使其看起來像對話框一樣。你也可以創建你自己的Theme.dialog作爲父項的主題。

+0

謝謝 - 我會嘗試Theme.Dialog – headscratch 2010-08-17 21:39:39

+0

嗨,你能舉個例子嗎? – RaagaSudha 2012-02-10 06:01:30

1

Android 1.6+爲您的活動提供了一組易用的對話框功能。

製作對話框時,只需在對話框的按鈕的onClick()方法中執行任何操作即可。你可以在DIALOG_CONFIRM_DELETE開關中看到我在下面看到的一個例子,其中我刪除了一個本地數據庫的記錄,並且查詢了一個遊標。

我想看看http://d.android.com開發指南中的「創建對話框」部分它向您展示瞭如何使用對話框功能。 http://developer.android.com/guide/topics/ui/dialogs.html

@Override 
protected Dialog onCreateDialog(int id) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 

    switch (id) { 
     case DIALOG_CONFIRM_DELETE: 
      builder 
       .setTitle("Confirm") 
       .setMessage("Are you sure you want to delete that access point?") 
       .setCancelable(true) 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         mDbAdapter.delete(SELECTED_AP_ID); 
         mCursor.requery(); 
        } 
       }).setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.dismiss(); 
        } 
       }); 
      return builder.create(); 
     case DIALOG_EXPORTING: 
      ProgressDialog progressDialog = new ProgressDialog(this); 
      progressDialog.setMessage("Exporting..."); 
      progressDialog.setCancelable(false); 
      progressDialog.show(); 
      return progressDialog; 
     case DIALOG_GPS_DISABLED: 
      builder 
       .setTitle("GPS signal not Found") 
       .setMessage("GPS is not enabled, and accuracy may be effected.") 
       .setCancelable(false) 
       .setNeutralButton("Ok", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         startService(); 
         dialog.dismiss(); 
        } 
       }); 
      return builder.create(); 
     default: 
      return null; 
    } 
} 
1

這是很遲,但希望這將幫助其他人的未來。 我所做的就是創建實現OnClickListener自定義對話框:

public class LoginDialog extends Dialog implements OnClickListener{ 

    public LoginDialog(Context context) { 
    super(context); 
    this.context = context; 
    // TODO Auto-generated constructor stub 

    } 

    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.dialog); 
    setTitle("Enter Login Details"); 
    setCancelable(true); 

    etUsername = (EditText) findViewById(R.id.etUsername); 
    etPassword = (EditText) findViewById(R.id.etPassword); 
    bLogin = (Button) findViewById(R.id.bDialogLogin); 
    bCancel = (Button) findViewById(R.id.bDialogCancel); 

    bLogin.setOnClickListener(this); 
    bCancel.setOnClickListener(this); 


    } 

    public void onClick(View v) { 

    switch (v.getId()) { 
    case R.id.bDialogLogin: 
     //Check credentials. If they pass: 
     cancel(); 

     //if they don't pass, throw an alert or something 
     break; 

    case R.id.bDialogCancel: 
     dismiss(); 
     break; 
    } 

    } 
} 

然後在我的主代碼我實現了一個onCancelListener(對話有onCancel和onDismiss聽衆都爲您準備)。

//This calls my alert dialog. Place this in the onClick of a button or something 
loginDialog = new LoginDialog(this); 
loginDialog.show(); 


loginDialog.setOnCancelListener(new OnCancelListener() { 

     @Override 
     public void onCancel(DialogInterface dialog) { 
      // TODO Auto-generated method stub 

      //This code is run whenever the cancel(); function in the dialog is called. 

     } 
    }); 

希望這有助於!