Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更改按钮';Alerts对话框中的文本_Java_Android_Android Alertdialog - Fatal编程技术网

Java 更改按钮';Alerts对话框中的文本

Java 更改按钮';Alerts对话框中的文本,java,android,android-alertdialog,Java,Android,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); build

我需要将
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();
}
在调试期间,我观察到
按钮positive
的文本值被适当地更改,但它没有反映在界面中。你知道为什么会这样吗?我查过了,但没用。

你可以试试这个

 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();
    }
public void show(){
视图输入视图=线性布局。充气(AppIntroActivity.this,R.layout.goal\u tag\u input,null);
AlertDialog.Builder=新建AlertDialog.Builder(AppIntroActivity.this);
builder.setView(inputView);
final EditText mInput=(EditText)inputView.findViewById(R.id.goal\u tag\u input);
final Button buttonPositive=(Button)inputView.findViewById(R.id.Button\u id);
addTextChangedListener(新的TextWatcher(){
@凌驾
public void beforeTextChanged(字符序列s、int start、int count、int after){}
@凌驾
public void onTextChanged(字符序列,int start,int before,int count){}
@凌驾
公共无效后文本已更改(可编辑){
字符串标记名=mInput.getText().toString();
如果(标记名.trim()!=“”){
if(mAvailableTagLabels.contains(标记名)){
buttonPositive.setText(R.string.OK);
}否则{
buttonPositive.setText(R.string.Add);
}
}
}
};);
List availableTags=AppDatabase.getInstance(mContext.tagDao().getAll();
mAvailableTagLabels=新ArrayList();
用于(标签标签:可用标签){
mavaailabletaglabels.add(tag.getName());
}
ArrayAdapter inputAdapter=新的ArrayAdapter(mContext,
android.R.layout.select_对话框_项,mavaailabletaglabels);
设置适配器(输入适配器);
builder.setCancelable(true);
builder.setPositiveButton(“”,mAddTagClickListener);
setNegativeButton(R.string.Cancel,null);
mDialog=builder.create();
mDialog.show();
}

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

你的意思是你想更改“是”或“否”文本???@MohammadAli我需要将正面按钮的文本从“无”更改为“确定”,或根据输入的文本进行添加。参考此链接:尝试此链接@MohammadAli谢谢,但这不是我的情况。我怎样才能得到按钮的id<代码>最终按钮buttonPositive=(按钮)inputView.findViewById(R.id.Button\u id)。一般来说,您是想在show方法中移动
TextWatcher
实现,还是想做什么?只需在对话框xml中添加按钮并在Listener中管理文本更改我不明白您的意思。您以前是否使用过
AlertDialog
?它没有布局。按钮是在这里创建的:
builder.setPositiveButton(“”,mAddTagClickListener)添加按钮只需管理文本使用此选项检查第二个答案,它显示了如何在不使用警报对话框生成器的情况下显示对话框
 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();
    }