我一直在試圖獲得對話框的句柄。我閱讀了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(); //顯示對話框 } //結束對話框 } //結束活動
您應該編輯您的問題,以便代碼正確顯示(確保每行前有4個空格)。那麼它會更容易幫助你:) – stealthcopter 2010-08-17 21:35:58