如何在收到来自Android的定制对话框返回的数据 [英] How to recieve returned data from custom Dialog in android

查看:123
本文介绍了如何在收到来自Android的定制对话框返回的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮和一个第一个活动edittextbox.when我点击该按钮我需要从第一个活动传递数据的列表,第二活性。第二个活动将显示,截至list.If用户选择从列表中。 ,该特定数据应回迁的被称为活动(第一项活动)和我能做到这一点应该显示在EditText上box.How?

我FirstActivity:

 公共类先伸出活动{    点击按钮;
    编辑的EditText;    ArrayList的<串GT;网站=新的ArrayList<串GT;();
    的String [] = sitestr新的String [] {猴子,驴,象,水牛};
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑冰柱){
        super.onCreate(冰柱);
        的setContentView(R.layout.main);
        点击=(按钮)findViewById(R.id.click);
        编辑=(EditText上)findViewById(R.id.edit);        click.setOnClickListener(新OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                  CustomizeDialog1 customizeDialog =新CustomizeDialog1(tu.this,sitestr);
                  customizeDialog.show();            }
        });
}

二:

 公共类CustomizeDialog1扩展对话框实现OnClickListener,OnItemClickListener {
  串SELECTED_VALUE;
  按钮okButton;
  串喜[];
// ListView控件list_view;  公共CustomizeDialog1(上下文的背景下,的String []值){
    超级(上下文);
    HI =价值;
   // Log.v(长度,+ hi.length);
    / **'Window.FEATURE_NO_TITLE - 用于隐藏标题* /
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    / **设计在对话框中main.xml中的文件* /
    的setContentView(R.layout.listviewinflate);    ListView的LST =(ListView控件)findViewById(R.id.list1);     ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(的getContext(),android.R.layout.simple_list_item_1,喜);     lst.setAdapter(适配器);
     lst.setOnItemClickListener(本);   okButton =(按钮)findViewById(R.id.cancel);
   okButton.setOnClickListener(本);
  }  @覆盖
  公共无效的onClick(视图v){
    / **当点击OK按钮,关闭对话框* /
    如果(V == okButton)
      解雇();
  }  @覆盖
  公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){
    // TODO自动生成方法存根
    SELECTED_VALUE =喜[ARG2]
    //Log.v(\"selected_value,SELECTED_VALUE);
    Toast.makeText(的getContext(),SELECTED_VALUE,Toast.LENGTH_SHORT).show();
}
}


解决方案

您可以自定义的回调接口是这样的。

 公共静态界面MyDialogListener
{
    公共无效userSelectedAValue(字符串值);
    公共无效userCanceled();
}

在您 CustomizeDialog1 这个声明为类成员,使二传手和放大器;吸气它。
然后在你的的onClick 活动

 公共无效的onClick(视图v){
    CustomizeDialog1 customizeDialog =新CustomizeDialog1(tu.this,sitestr);
    customizeDialog.setMyDialogListener(新MyDialogListener()
    {
    公共无效userSelectedAValue(字符串值)
    {
        //使用价值
    }
    公共无效userCanceled()
    {
    }
    });
    customizeDialog.show();}

和你 CustomizeDialog1 当用户preSS 确定按钮。

 公共无效的onClick(视图v)
{
    / **当点击OK按钮,关闭对话框* /
    如果(V == okButton)
    {
        listener.userSelectedAValue(SELECTED_VALUE);
        //监听器是你MyDialogListener,你们从设置的对象
        //活动。
        解雇();
    }
}

I have first activity with one button and one edittextbox.when i click that button i need to pass list of data from first activity to second activity.The second activity will displays that as list.If user selects one from the list.,that particular data should move back to the called activity(First activity) and should be displayed on the EditText box.How could i do that?

My FirstActivity:

public class First extends Activity {

    Button click;
    EditText edit;

    ArrayList<String> site=new ArrayList<String>();
    String[] sitestr=new String[]{"monkey","donkey","Elephant","Baffalo"};


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        click=(Button)findViewById(R.id.click);
        edit=(EditText)findViewById(R.id.edit);

        click.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


                  CustomizeDialog1 customizeDialog = new CustomizeDialog1(tu.this,sitestr);
                  customizeDialog.show();

            }
        });
}

Second:

public class CustomizeDialog1 extends Dialog implements OnClickListener, OnItemClickListener {
  String selected_value;
  Button okButton;
  String hi[];
//  ListView list_view;

  public CustomizeDialog1(Context context,String[] value) {
    super(context);
    hi=value;
   // Log.v("Length",""+hi.length);
    /** 'Window.FEATURE_NO_TITLE' - Used to hide the title */
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    /** Design the dialog in main.xml file */
    setContentView(R.layout.listviewinflate);

    ListView lst=(ListView)findViewById(R.id.list1);

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1,hi);

     lst.setAdapter(adapter);
     lst.setOnItemClickListener(this);

   okButton = (Button) findViewById(R.id.cancel);
   okButton.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    /** When OK Button is clicked, dismiss the dialog */
    if (v == okButton)
      dismiss();
  }

  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    selected_value=hi[arg2];
    //Log.v("selected_value", selected_value);
    Toast.makeText(getContext(), selected_value,Toast.LENGTH_SHORT).show();
}
}

解决方案

You can have custom callback interface like this.

public static interface MyDialogListener
{
    public void userSelectedAValue(String value);
    public void userCanceled();
}

Declare this in you CustomizeDialog1 as class member and make setter & getter for it. Then in your onClick inside Activity:

public void onClick(View v){
    CustomizeDialog1 customizeDialog = new CustomizeDialog1(tu.this,sitestr);
    customizeDialog.setMyDialogListener( new MyDialogListener()
    {
    public void userSelectedAValue(String value)
    {
        //use value
    }
    public void userCanceled()
    {
    }
    });
    customizeDialog.show();

}

and in you CustomizeDialog1 when user press OK button.

public void onClick(View v)
{
    /** When OK Button is clicked, dismiss the dialog */
    if (v == okButton)
    {
        listener.userSelectedAValue(selected_value);
        // listener is object of your MyDialogListener, which you have set from
        // Activity.
        dismiss();
    }
}

这篇关于如何在收到来自Android的定制对话框返回的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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