view.getText()。toString()从DialogFragment返回什么?

时间:2018-11-28 11:51:05

标签: android

我有一个Custom DialogFragment类。用户在2个editText视图中键入两次密码。当我记录操作时,即使我在两个都输入了一些文本,我也从两个editTexts中得到了一个空文本:

  

D / CreateNewWalletDialogFragment:密码:重复密码:

public class CreateNewWalletDialogFragment extends DialogFragment {

    CreateNewWalletDialogListener createNewWalletDialogListener;

    @BindView(R.id.password_edit)
    TextView password;
    @BindView(R.id.password_edit_repeat)
    TextView password_repeat;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try{
            createNewWalletDialogListener = (CreateNewWalletDialogListener) context;
        }catch (ClassCastException e) {
            throw new ClassCastException(getActivity().toString());
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        ButterKnife.bind(this, View.inflate(getContext(), R.layout.create_wallet_popup, null));
        builder.setView(inflater.inflate(R.layout.create_wallet_popup, null))
                .setPositiveButton(R.string.create_wallet_button, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        String password_s = password.getText().toString();
                        String password_repeat_s = password_repeat.getText().toString();
                        Timber.d("password: "+password_s+" password repeat: "+ password_repeat_s);

                    }
                });
        return builder.create();
    }
}

根据某人的建议,用onStart()方法绑定视图也无济于事。有什么建议吗?

2 个答案:

答案 0 :(得分:2)

我认为这应该是EditText,而不是TextView。

尝试

@BindView(R.id.password_edit)
EditText password;
@BindView(R.id.password_edit_repeat)
EditText password_repeat;

答案 1 :(得分:1)

问题出在这里:

ButterKnife.bind(this, View.inflate(getContext(), R.layout.create_wallet_popup, null));
builder.setView(inflater.inflate(R.layout.create_wallet_popup, null))

是否注意到您有两个inflate?每次调用Inflates都会返回一个新实例,因此,就您而言,您在屏幕上提示的视图与ButterKnife绑定的视图不同。尝试

 View view = View.inflate(getContext(), R.layout.create_wallet_popup, null);
 ButterKnife.bind(this, view);
 builder.setView(view)