【问题标题】:Change button's text in AlertDialog在 AlertDialog 中更改按钮的文本
【发布时间】:2018-06-08 05:28:59
【问题描述】:

我需要根据输入的文本将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,但没有帮助。

【问题讨论】:

标签: java android android-alertdialog


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    你可以试试这个

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

    【讨论】:

    • 如何获取按钮的 id? final Button buttonPositive = (Button) inputView.findViewById(R.id.button_id);。一般而言,您是否只想在 show 方法中移动 TextWatcher 实现,或者您的意图是什么?
    • 只需在对话框 xml 中添加按钮并管理侦听器中的文本更改
    • 我不明白你的意思。您以前与AlertDialog 合作过吗?它没有布局。按钮在这里创建:builder.setPositiveButton("", mAddTagClickListener);
    • 在goal_tag_input布局和文本更改监听器中添加按钮只管理文本
    • 使用 this 检查第二个答案,它显示了如何在不使用警报对话框构建器的情况下显示对话框
    猜你喜欢
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 2021-04-07
    相关资源
    最近更新 更多