Android的对话框 - 迷茫 [英] Android Dialog - confused

查看:112
本文介绍了Android的对话框 - 迷茫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让对话框的句柄。我读了Android开发人员网站信息和十几个bolg的。这似乎有这样做的几种方法,我至少能够得到一个对话框,3个按键(不使用带有自定义布局的自定义对话框)。

I've been trying to get a handle on Dialogs. I read the android dev site info and a dozen bolg's. It seems there are several ways of doing it and I'm able to at least get a dialog with 3 buttons (not using a custom dialog with custom layout).

如果我成立了正/负/中性))像完成(,取消(等行动,它与那些电话。

If I set up the positive/negative/neutral actions with something like finish(), cancel() etc, it works with those calls.

但是,我想要做的是有按钮做更多的东西,如果仅仅使用显示在主code(不是吐司)中定义的字符串文本。最后,我想在一个对话框中输入一些数字和字符串归还。是的,我能做到这一点,从另一个活动屏幕,但是,preFER不是因为我喜欢一个对话框的紧凑尺寸。

But, what I want to do is have the buttons do something more, if only display a text using a string defined in the Maincode (not a Toast). Eventually, I want to enter some numbers in a dialog and return them in a string. Yes, I can do that from another activity screen but, prefer not to as I like the compact size of a dialog.

甚至欺骗并返回一个整数主code做一些开关/箱的东西会好起来的,但是,我似乎无法连返回一个整数。

Even cheating and returning an integer to the Maincode to do some switch/case stuff would be okay but, I seem not able to even return an integer.

据我了解,我需要做一个定制的警告对话框做输入的东西,以下是我尝试在一开始通过只是想返回一个字符串或整数 - 它似乎并没有被让我有!

I understand that I'll need to do a customized alert dialog to do input stuff and the following is my attempt at a start by just trying to return a string or integer - it doesn't seem to be getting me there!

这code presents用3个按钮对话框。这仅仅是一个尝试的我做了(整数返回的东西删除)...

This code presents the dialog with 3 buttons. This is just one of the try's I made (integer return stuff deleted)...

我能做些什么,以从对话框按钮,返回到主code整数?有没有code的错误或警告,它只是没有像我希望的工作...

What can I do to return an integer to the Maincode from a dialog button? There are no code errors or warnings, it just doesn't work as I hoped...

    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(); //显示对话框
    } //结束对话
    } //结束活动

builder.show(); // show the dialog }//end Dialog }//end activity

推荐答案

您可以实现对话作为单独的活动,让你需要的任何行为。只是采用标准的Andr​​oid Theme.Dialog主题活动,以使它看起来像对话框。你也可以创建自己的指点主题Theme.Dialog父。

You can implement the dialog as separate Activity and get any behavior you need. Just apply standard android Theme.Dialog theme to the activity to make it look like dialog. Also you can create theme of your own pointing Theme.Dialog as parent.

这篇关于Android的对话框 - 迷茫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆