我一直在试图获得对话框的句柄。我阅读了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