沟通我的主要活动对话 [英] Communicating my Dialog with Main Activity

查看:89
本文介绍了沟通我的主要活动对话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读了很多关于如何与活动通信的FragmentDialog教程,但我似乎无法使其适应我的项目。

我想要做的就是简单,当用户点击我的FragmentDialog我想打电话给在主要活动的方法的积极按钮。一个简单的想法,但执行的是杀了我。

我的主要活动采用标签式浏览,这是我的东西是搞乱孔事情了。

这是我的主要活动的短版。

 公共类MainActivity扩展ActionBarActivity实现ActionBar.TabListener {   // .....    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();        // noinspection SimplifiableIfStatement        如果(ID == R.id.menu_change_date){
            DialogFragment对话框=新Dialog_Elegir_Mes();
            dialog.show(getSupportFragmentManager(),Elegir的Mes);
        }  // ....  公共无效DoSomething的(){   } }

对话框

 公共类Dialog_Elegir_Mes扩展DialogFragment {    @覆盖
    公共对话框onCreateDialog(捆绑savedInstanceState){
        //生成对话框,设置按钮单击处理程序
        AlertDialog.Builder ADB =新AlertDialog.Builder(getActivity());
        LayoutInflater吹气= getActivity()getLayoutInflater()。        最后的视图V = inflater.inflate(R.layout.diag_select_month,NULL);
        adb.setTitle(Elegir的Mes);        adb.setView(ⅴ)
                .setPositiveButton(Aceptar,新DialogInterface.OnClickListener(){
                    公共无效的onClick(DialogInterface对话,诠释的id){                          //这里调用DoSomething的();
                    }
                })
                .setNegativeButton(Cancelar,新DialogInterface.OnClickListener(){
                    公共无效的onClick(DialogInterface对话,诠释的id){
                        Dialog_Elegir_Mes.this.getDialog()取消()。
                    }
                });
        返回adb.create();
    }
}


解决方案

创建一个接口和实施,在Activity类,并通过其参考Dialog类。当用户点击对话框按钮使用该接口调用的方法。

做这样的事情。

 公共接口OnMyDialogClick
{
    公共抽象无效onPositiveButtonClick();
}

您活动

 如果(ID == R.id.menu_change_date){
        DialogFragment对话框=新Dialog_Elegir_Mes(新OnMyDialogClick()
        {
              @覆盖
              公共无效onPositiveButtonClick()
              {
                   //调用您的活动方法在这里
              }
        });
        dialog.show(getSupportFragmentManager(),Elegir的Mes);
}

对话框类

 公共类Dialog_Elegir_Mes扩展DialogFragment {
私人OnMyDialogClick _onMyDialogClick = NULL;
公共Dialog_Elegir_Mes(OnMyDialogClick REF)
{
    _onMyDialogClick = REF;
}@覆盖
公共对话框onCreateDialog(捆绑savedInstanceState){
    //生成对话框,设置按钮单击处理程序
    AlertDialog.Builder ADB =新AlertDialog.Builder(getActivity());
    LayoutInflater吹气= getActivity()getLayoutInflater()。    最后的视图V = inflater.inflate(R.layout.diag_select_month,NULL);
    adb.setTitle(Elegir的Mes);    adb.setView(ⅴ)
            .setPositiveButton(Aceptar,新DialogInterface.OnClickListener(){
                公共无效的onClick(DialogInterface对话,诠释的id){                      _onMyDialogClick.onPositiveButtonClick();
                }
            })
            .setNegativeButton(Cancelar,新DialogInterface.OnClickListener(){
                公共无效的onClick(DialogInterface对话,诠释的id){
                    Dialog_Elegir_Mes.this.getDialog()取消()。
                }
            });
    返回adb.create();
}
}

I have read a lot of tutorials on how to communicate a FragmentDialog with the activity but I seem be unable to adapt them to my project.

What I want to do is simple, when the user clicks the positive Button on my FragmentDialog I want to call a method in main activity. A simple idea, but execution is killing me.

My main Activity uses tabbed browsing, which I thing is messing the hole thing up.

This is a short version of my main activity.

public class MainActivity extends ActionBarActivity implements ActionBar.TabListener  {

   //.....

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement

        if(id == R.id.menu_change_date){
            DialogFragment dialog = new Dialog_Elegir_Mes();
            dialog.show(getSupportFragmentManager(),"Elegir Mes");
        }

  //....

  public void dosomething() {

   }

 }

Dialog

public class Dialog_Elegir_Mes extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Build the dialog and set up the button click handlers
        AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();

        final View v = inflater.inflate(R.layout.diag_select_month,null);


        adb.setTitle("Elegir Mes");

        adb.setView(v)
                .setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                          //Here call dosomething();
                    }
                })
                .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Dialog_Elegir_Mes.this.getDialog().cancel();
                    }
                });


        return adb.create();
    }
}

解决方案

Create an Interface and implement that in the Activity class and pass its reference to the Dialog class. When user clicks the dialog button call the method using the interface.

Do something like this

public interface OnMyDialogClick
{
    public abstract void onPositiveButtonClick();
}

Your activity

if(id == R.id.menu_change_date){
        DialogFragment dialog = new Dialog_Elegir_Mes(new OnMyDialogClick()
        {
              @Override
              public void onPositiveButtonClick()
              {
                   //Call your activity method here
              }
        });
        dialog.show(getSupportFragmentManager(),"Elegir Mes");
}

Your Dialog class

public class Dialog_Elegir_Mes extends DialogFragment {
private OnMyDialogClick _onMyDialogClick = null;
public Dialog_Elegir_Mes(OnMyDialogClick ref)
{
    _onMyDialogClick = ref;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Build the dialog and set up the button click handlers
    AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    final View v = inflater.inflate(R.layout.diag_select_month,null);


    adb.setTitle("Elegir Mes");

    adb.setView(v)
            .setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                      _onMyDialogClick.onPositiveButtonClick();
                }
            })
            .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Dialog_Elegir_Mes.this.getDialog().cancel();
                }
            });


    return adb.create();
}
}

这篇关于沟通我的主要活动对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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