在Android中按下按钮时如何创建弹出窗口?

在Android中按下按钮时如何创建弹出窗口?,android,Android,如何创建接受简单字符串消息的自定义弹出类?我是安卓新手,如果您有代码方面的帮助,我们将不胜感激 当按下主布局中的按钮时,屏幕上必须弹出弹出窗口 自定义弹出类 public class CustomPopup extends PopupWindow { private String message; private Double anchorX; private Double anchorY; PopupWindow popup; public Cus

如何创建接受简单字符串消息的自定义弹出类?我是安卓新手,如果您有代码方面的帮助,我们将不胜感激

当按下主布局中的按钮时,屏幕上必须弹出弹出窗口

自定义弹出类

public class CustomPopup extends PopupWindow {

    private String message;
    private Double anchorX;
    private Double anchorY;

    PopupWindow popup;

    public CustomPopup(String message) {
        super();
        this.message = message;
    }

    public void showPopup(Activity context) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
}
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText messageTxt = (EditText) findViewById(R.id.messageTxt);
        Button generateBtn = (Button) findViewById(R.id.generateBtn);

        String message = messageTxt.getText().toString();

        final CustomPopup popup = new CustomPopup(message);

        generateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popup.showPopup();
            }
        });
    }
}
主类

public class CustomPopup extends PopupWindow {

    private String message;
    private Double anchorX;
    private Double anchorY;

    PopupWindow popup;

    public CustomPopup(String message) {
        super();
        this.message = message;
    }

    public void showPopup(Activity context) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
}
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText messageTxt = (EditText) findViewById(R.id.messageTxt);
        Button generateBtn = (Button) findViewById(R.id.generateBtn);

        String message = messageTxt.getText().toString();

        final CustomPopup popup = new CustomPopup(message);

        generateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popup.showPopup();
            }
        });
    }
}

您可以创建自定义xml布局

在按钮的OnClickListener中,您可以放置以下内容:

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        alertLayout = inflater.inflate(R.layout.YOUR_CUSTOM_POPUP_LAYOUT, null);

        final AlertDialog alert = new AlertDialog.Builder(this).create();
        alert.setView(alertLayout);
        TextView msg= alertLayout.findViewById(R.id.YOUR_TEXTVIEW_ID);

        alert.show();

之后,您可以在弹出窗口中添加另一个按钮,并在其上设置一个侦听器,以在单击后关闭布局。

您可以根据需要更改以下代码。这只是一个如何创建和实现自定义
对话框片段的示例

@Override
public void onFinishOkCancelDialog(boolean submit) {
    if(submit){
        // Do something positive
    }
    else{
        // Do something negative
    }
}
他就是我使用的密码。我发现它非常灵活,因为您可以为稍微不同的任务创建几个类似的对话框。您将需要创建一个布局文件-这在功能和样式上为您提供了很大的灵活性

我的布局文件是“碎片\u确定\u取消\u”对话框

要满足您的需求,只需创建自己的布局文件,其中包含您需要的所有元素(如图像)

在调用对话框的活动中,需要实现侦听器

implements OkCancelDialogFragment.OkCancelDialogListener
另一个优点是,使用我的代码,您可以更改标题和消息,以满足任何活动的需要

private void callMyDialog(){ 
    //Customize the title and message as needed
    String title = "This is my dialog title";
    String mess = "This is my dialog message";
    OkCancelDialogFragment dialog = OkCancelDialogFragment.newInstance(title, mess);
    dialog.show(getFragmentManager(), "OkCancelDialogFragment2");
}
现在,您需要在调用
DialogFragment
的活动中实现对话框回调

@Override
public void onFinishOkCancelDialog(boolean submit) {
    if(submit){
        // Do something positive
    }
    else{
        // Do something negative
    }
}
现在是DialogFragment的代码:

public class OkCancelDialogFragment extends DialogFragment {

    private static final String ARG_TITLE = "title";
    private static final String ARG_MESSAGE = "message";

    Context context = null;

    private String title;
    private String message;
    private boolean submitData = false;

    private OkCancelDialogListener mListener;

    public OkCancelDialogFragment() {
    }

    public static OkCancelDialogFragment newInstance(String title, String message) {
        OkCancelDialogFragment fragment = new OkCancelDialogFragment();
        Bundle args = new Bundle();
        args.putString(ARG_TITLE, title);
        args.putString(ARG_MESSAGE, message);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            title = getArguments().getString(ARG_TITLE);
            message = getArguments().getString(ARG_MESSAGE);
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle saveIntsanceState){

        context = getActivity();

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();

        View rootView = inflater.inflate(R.layout.fragment_ok_cancel_dialog, null, false);
        final TextView titleView = (TextView)rootView.findViewById(R.id.tvTitle);
        final TextView messView = (TextView)rootView.findViewById(R.id.tvMessage);

        titleView.setText(title);
        messView.setText(message);

        builder.setView(rootView)
//                .setTitle(title)
                .setPositiveButton(R.string.ok_button_dialog_title, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        submitData = true;
                        if(mListener == null) mListener = (OkCancelDialogListener) context;
                        mListener.onFinishOkCancelDialog(submitData);
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        submitData = false;
                        if(mListener == null) mListener = (OkCancelDialogListener) context;
                        mListener.onFinishOkCancelDialog(submitData);
                    }
                });
        return builder.create();
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            if(mListener == null) mListener = (OkCancelDialogListener) context;
        }
        catch (Exception ex){
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    public interface OkCancelDialogListener {
        void onFinishOkCancelDialog(boolean submit);
    }

}

请注意.setTitle(title)对API 23或更高版本(或者可能是API 21或更高版本?)有效。

没有人会为您编写此代码,主要是因为没有人知道您需要做什么。你需要展示你的尝试,告诉我们你得到了什么结果,这是一个问题。写一些代码,找出它。没有代码,没有人能帮助你。用你的一些代码编辑你的问题,这样人们就可以提供帮助。我已经添加了到目前为止我尝试过的内容