Android Button 文本在点击时未更新



尝试在 XML 中的 onClick() 中按下按钮后更改按钮上的文本。但是运行该方法后文本不会更新,只有在我再次单击按钮后,它才会更新为新文本。我尝试使用runOnUiThread()运行它,但它的行为相同。有人知道为什么吗?

法典:

public void setLabel(View v) {
    v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
    final EditText input = new EditText(this);
    new AlertDialog.Builder(this)
            .setMessage("Add label:")
            .setView(input)
            .setPositiveButton("Set", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    if (input.getText() == null || input.getText().toString().length() == 0) {
                        Toast.makeText(AlarmDialog.this, Html.fromHtml("<i>" + getString(R.string.label_reset) + "<i>"), Toast.LENGTH_SHORT).show();
                        labelSet = false;
                    }
                    else {
                        alarmLabel = input.getText().toString();
                        labelSet = true;
                        Toast.makeText(AlarmDialog.this, Html.fromHtml("<b>" + getString(R.string.label_set) + "<b>"), Toast.LENGTH_SHORT).show();
                    }
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    Toast.makeText(AlarmDialog.this, Html.fromHtml("<i>" + getString(R.string.label_not_set) + "<i>"), Toast.LENGTH_SHORT).show();
                }
            }).create().show();
    Button button = (Button) v;
    if (labelSet)
        button.setText(R.string.label_set);
    else
        button.setText(R.string.label);
}

.XML:

<Button
            android:id="@+id/label_button"
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="@string/label"
            android:onClick="setLabel"
            style="@android:style/Widget.Holo.Button.Borderless"
            android:padding="8dp"/>
我认为

问题是你的

Button button = (Button) v;
if (labelSet)
    button.setText(R.string.label_set);
else
    button.setText(R.string.label);

代码在警报对话框中输入或单击某些内容之前执行。因为代码就在你说 AlertDialog.show() 的那一行下面。因此,如果您希望之后执行代码,则必须将其添加到onClickListener中。第二次它之所以有效,只是因为在再次单击按钮之前关闭了警报视图。并且您的标签集变量已经设置好了。

如果labelSet最初设置为 false ,那么第一次单击按钮时...

button.setText(R.string.label);

。将被调用,这不会更新按钮中的文本。

请记住,AlertDialog 的onClick方法将在setLabel方法运行完成调用(具体而言,当您单击AlertDialog中的正按钮时将调用该方法)。我相信你正在寻找的更像是...

public void setLabel(View v) {
    v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
    final Button button = (Button) v;
    final EditText input = new EditText(this);
    new AlertDialog.Builder(this)
        .setMessage("Add label:")
        .setView(input)
        .setPositiveButton("Set", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                if (input.getText() == null || input.getText().toString().length() == 0) {
                    Toast.makeText(AlarmDialog.this, Html.fromHtml("<i>" + getString(R.string.label_reset) + "<i>"), Toast.LENGTH_SHORT).show();
                    button.setText(R.string.label);
                }
                else {
                    alarmLabel = input.getText().toString();
                    Toast.makeText(AlarmDialog.this, Html.fromHtml("<b>" + getString(R.string.label_set) + "<b>"), Toast.LENGTH_SHORT).show();
                    button.setText(R.string.label_set);
                }
            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                Toast.makeText(AlarmDialog.this, Html.fromHtml("<i>" + getString(R.string.label_not_set) + "<i>"), Toast.LENGTH_SHORT).show();
            }
        }).create().show();
}

最新更新