如何从MainActivity(class)中的AlertDialog(class)框中接收值,而无需将控制权传递给Al



描述:我已经创建了AlertDialogBox Class,当我需要弹出对话框时,我使用alertDialog.show(),对话框弹出窗口将输入作为是/否(按钮为正和负)并保持在同一活动上,但是我如何获取值,即是或否,并从对话框中传递用户在mainActivity中输入的值。尝试过:- 我尝试使用捆绑包,使用 put/get 和键值,但它返回空值。尝试使用全局变量,但仍然是空值。

感谢您的帮助

您可以使用

接口以及DialogFragment类的getActivity()getParentFragment()方法来执行此操作。

注意:我假设(因为您没有发布任何代码)您已经在使用对话片段来显示警报对话。

首先创建一个对话框和一个特殊的接口,可用于将值传递给拥有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);
        }
    }
}

然后将特殊界面添加到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
    }
}

主要活动:

private void 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;
        }
    });
}

警报对话框类

public class AlertDialogFragment extensions 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;
}

}

那么,我应该如何将从 AlertDialogFragment 类收到的"yes"值传递到主活动中,而无需传递意图或控制。

相关内容

  • 没有找到相关文章

最新更新