在AlertDialog中更改按钮的文本

时间:2018-05-31 12:24:10

标签: java android alertdialog android-alertdialog

我需要根据输入的文字,将AlertBox的正面按钮文本从空状态更改为“确定”或“添加”。我有AlertBox创建和显示的以下代码:

public void show() {
    View inputView = LinearLayout.inflate(mContext, R.layout.goal_tag_input, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
    builder.setView(inputView);
    mInput = inputView.findViewById(R.id.goal_tag_input);
    mInput.addTextChangedListener(mTagNameTextWatcher);
    List<Tag> availableTags = AppDatabase.getInstance(mContext).tagDao().getAll();
    mAvailableTagLabels = new ArrayList<>();
    for (Tag tag : availableTags) {
        mAvailableTagLabels.add(tag.getName());
    }
    ArrayAdapter<String> inputAdapter = new ArrayAdapter<>(mContext, 
    android.R.layout.select_dialog_item, mAvailableTagLabels);
    mInput.setAdapter(inputAdapter);
    builder.setCancelable(true);
    builder.setPositiveButton("", mAddTagClickListener);
    builder.setNegativeButton(R.string.Cancel, null);
    mDialog = builder.create();
    mDialog.show();
}

我也有TextWatcher实施:

private TextWatcher mTagNameTextWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {}

    @Override
    public void afterTextChanged(Editable s) {
        String tagName = mInput.getText().toString();
        if (tagName.trim() != "") {
            Button buttonPositive = mDialog.getButton(DialogInterface.BUTTON_POSITIVE);
            if (mAvailableTagLabels.contains(tagName)) {
                buttonPositive.setText(R.string.OK);
            } else {
                buttonPositive.setText(R.string.Add);
            }
        }
    }
};

在调试期间,我发现buttonPositive的文本值已正确更改,但未在界面中反映出来。你有什么想法为什么会这样?我检查了this answer,但没有帮助。

2 个答案:

答案 0 :(得分:0)

你可以试试这个

 public void show() {
        View inputView = LinearLayout.inflate(AppIntroActivity.this, R.layout.goal_tag_input, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(AppIntroActivity.this);
        builder.setView(inputView);
       final EditText mInput =(EditText) inputView.findViewById(R.id.goal_tag_input);
        final Button buttonPositive = (Button) inputView.findViewById(R.id.button_id);
        mInput.addTextChangedListener( new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable s) {
                String tagName = mInput.getText().toString();
                if (tagName.trim() != "") {
                    if (mAvailableTagLabels.contains(tagName)) {
                    buttonPositive.setText(R.string.OK);
                } else {
                    buttonPositive.setText(R.string.Add);
                }
                }
            }
        };);
        List<Tag> availableTags = AppDatabase.getInstance(mContext).tagDao().getAll();
        mAvailableTagLabels = new ArrayList<>();
        for (Tag tag : availableTags) {
            mAvailableTagLabels.add(tag.getName());
        }
        ArrayAdapter<String> inputAdapter = new ArrayAdapter<>(mContext,
                android.R.layout.select_dialog_item, mAvailableTagLabels);
        mInput.setAdapter(inputAdapter);
        builder.setCancelable(true);
        builder.setPositiveButton("", mAddTagClickListener);
        builder.setNegativeButton(R.string.Cancel, null);
        mDialog = builder.create();
        mDialog.show();
    }

答案 1 :(得分:0)

好吧,问题出现在使用AlertDialog构建器设置正面按钮:builder.setPositiveButton("", mAddTagClickListener);。显然,如果您将空字符串作为第一个参数传递,则不会创建按钮。我试图将其更改为(至少)一个空格字符串的那一刻 - 一切都开始按预期工作。
后来我改变了启用/禁用按钮的方法。

相关问题