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

时间:2017-11-02 14:55:26

标签: android

如何创建接受简单字符串消息的自定义弹出类?我是Android新手,对代码的帮助将不胜感激。

当在主布局中按下按钮时,弹出窗口必须弹出。

自定义弹出类

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();
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

您可以创建自定义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();

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

答案 1 :(得分:0)

您可以根据需要更改以下代码。这只是您制作和实施自定义DialogFragment的一个示例。

他是我使用的代码。我发现它非常灵活,因为您可以为略有不同的任务创建几个类似的对话框。您需要创建一个布局文件 - 这为您提供了很大的功能和风格灵活性。

我的布局文件是fragment_ok_cancel_dialog。

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

在调用对话框的Activity中,您需要实现Listener。

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

的Activity中实现对话框回调
@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(标题)对API 23或更高版本(或者可能是API 21或更高版本?)有效。