单击该片段内的按钮后,关闭该片段 [英] Close a fragment on button click which is inside that fragment

查看:90
本文介绍了单击该片段内的按钮后,关闭该片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个按钮的片段-确认和取消.一个按钮,即确认按钮,从EditText获取用户输入并将其转换为USSD请求.现在,我想像"alertDialog"上的否定按钮一样,使取消"按钮关闭该片段.

I have a fragment with two buttons - confirm and cancel. One button, the confirm button, takes the user input from an EditText and convert it to an USSD request. Now I'd like to have the cancel button close that fragment like the negative button on an alertDialog.

我读过许多与此类似的问题,但是似乎没有一个满足我的需求.

I've read many questions a bit similar to this but none of them seems to fullfil my need.

public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirmButton:

                if (inputField.getText().toString().trim().length() == 16) {
                    load();
                    inputField.requestFocus();
                } else if (inputField.getText().toString().trim().length() < 16) {
                    Toast.makeText(getActivity(), R.string.toast_number_not_valid, Toast.LENGTH_SHORT).show();
                }

                break;

            case R.id.cancelButton:
                dismiss();
                break;
        }
    }

 private void load() {
// bla - bla - bla
}

public void dismiss(){
//dear fragment, I don't need you right now, just dismiss
}

在这里,我需要使用dismiss()方法.

Here I need the dismiss() method.

任何帮助将不胜感激.

推荐答案

只需调用:

getActivity().onBackPressed();

这篇关于单击该片段内的按钮后,关闭该片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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