简体   繁体   English

我如何在MainActivity(class)的AlertDialog(class)框中接收值而不将控制权传递给AlertDialog类(Android Development)

[英]how can i receive value from AlertDialog(class) box in MainActivity(class) without passing control to AlertDialog class(Android Development)

Description: I have created AlertDialogBox Class, when i need the dialog box to pop-up i use alertDialog.show(), dialog box pop-up takes input as YES/NO(button as positive and negative) and stay on the same activity, but how can i get the value ie yes or no , and pass value enter by the user in the mainActivity from the dialog box. 说明:我创建了AlertDialogBox类,当我需要弹出对话框时,我使用alertDialog.show(),对话框弹出窗口将输入设为YES / NO(按钮为正和负),并保持相同的活动,但是我如何获取值,即yes或no,并通过对话框中的mainActivity用户输入值。 Tried:- I tried using bundle, using put/get and key value, but its return null value. 尝试:-我尝试使用bundle,使用put / get和键值,但它返回null值。 tried using global variable but still null value. 尝试使用全局变量,但仍然为空值。

Thanks for the help 谢谢您的帮助

You could do this by using an interface and the getActivity() and getParentFragment() methods of the DialogFragment class. 您可以通过使用接口以及DialogFragment类的getActivity()getParentFragment()方法来执行此DialogFragment

Note: I'm assuming (since you didn't post any code) that your already using a DialogFragment to show the AlertDialog. 注意:我假设(因为您没有发布任何代码)您已经在使用DialogFragment显示AlertDialog。

First create the dialog and a special interface which can be used to pass the value to the owning Activity or Fragment : 首先创建对话框和一个特殊的界面,该界面可用于将值传递给所属的ActivityFragment

public class MyAlertDialog extends DialogFragment implements DialogInterface.OnClickListener {

    public interface MyAlertDialogResultInterface {
        abstract void onButtonClicked(int button);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("Test message");
        builder.setPositiveButton("Ok", this);
        builder.setNegativeButton("No", this);
        return builder.create();
    }

    @Override
    public void onClick(DialogInterface dialogInterface, int button) {
        //Check if this DialogFragment is owned by a parent fragment
        if(getParentFragment() instanceof MyAlertDialogResultInterface){
            ((MyAlertDialogResultInterface) getParentFragment()).onButtonClicked(button);

        //Else check if this DialogFragment is owned by a parent activity
        } else if(getActivity() instanceof MyAlertDialogResultInterface){
            ((MyAlertDialogResultInterface) getActivity()).onButtonClicked(button);
        }
    }
}

Than add the special interface to your Activity and use the FragmentManager to show the dialog: 比将特殊接口添加到您的Activity并使用FragmentManager显示对话框:

public class Test extends Activity implements MyAlertDialog.MyAlertDialogResultInterface {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Set up layout
    }

    private void showAlertDialog(){
        new MyAlertDialog().show(getFragmentManager(), "dialog-tag");
    }

    @Override
    public void onButtonClicked(int button) {
        //Do what ever you want to do
    }
}

Main Activity: 主要活动:

private void registerClickCallBack(){ 私有无效registerClickCallBack(){

    ListView list = (ListView)findViewById(R.id.starListView);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        StarClassValue clickedPosition = myStar.get(position);

        Str = clickedPosition.getStarName();
        String intr = clickedPosition.getIconNum() +"";
            //Toast.makeText(MainActivity.this,Str + intr,Toast.LENGTH_LONG).show();
            sendMessage(Str,intr);


        }
    });

    list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            StarClassValue click = myStar.get(position);
            String str = click.getStarName();


         dialog.show(getFragmentManager(),"Favourite");

            return false;
        }
    });
}

AlertDialog Class AlertDialog类

public class AlertDialogFragment extends DialogFragment { 公共类AlertDialogFragment扩展DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Context context = getActivity();
    final AlertDialog.Builder builder = new AlertDialog.Builder(context);

    builder.setTitle("Add to Favorite").setMessage("Do you want to add this artist to your favourite list").setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    }).setNegativeButton("Cancel", null);
    AlertDialog dialog = builder.create();


    return dialog;
}

} }

So, how should i pass "yes" value received from AlertDialogFragment class into main activity without passing intent or control. 因此,如何在不传递意图或控制的情况下,将从AlertDialogFragment类接收的“是”值传递给主活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM