【问题标题】:Change values (colors) of TextView with a button click通过单击按钮更改 TextView 的值(颜色)
【发布时间】:2014-05-16 14:29:13
【问题描述】:

所以当我点击特定按钮时,我试图让这个 EditText 从一个 textColor 变为另一个(例如:button1 将 textcolor 更改为 BLUE,button2 将其更改为 GREEN 等)

 <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/test"
    android:id="@+id/test"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textColor="@color/orange_test"
    android:layout_toLeftOf="@+id/colorBtn"
    android:layout_alignBottom="@+id/colorBtn"
    android:textSize="25sp"
    android:gravity="center_vertical|center_horizontal" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/colorBtn"
    android:id="@+id/colorBtn"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="changeColor"/>

我应该如何继续? :\

【问题讨论】:

  • 查看OnClickListeners

标签: android android-button textcolor


【解决方案1】:

复制您的按钮以设置多种颜色

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/colorBtn"
    android:id="@+id/colorBtn"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="changeColor"
/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/colorBtnRed"
    android:id="@+id/colorBtnRed"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="changeColor"
/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/colorBtnGreen"
    android:id="@+id/colorBtnGreen"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="changeColor"
/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/colorBtnBlue"
    android:id="@+id/colorBtnBlue"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="changeColor"
/>

在您的 changeColor 中,检查按钮 ID。如果是 colorButtonBlue id,则将 EditText 的文本颜色设置为蓝色。如果是colorButtonRed,则设置为red abd,以此类推……

public void changeColor(View v)
{
    switch(v.getId())
    {
        case R.id.colorBtnRed:
            edtTest.setTextColor(Color.RED);
            break;
        case R.id.colorBtnGreen:
            edtTest.setTextColor(Color.GREEN);
            break;
        case R.id.colorBtnBlue:
            edtTest.setTextColor(Color.BLUE);
            break;
        default:
            edtTest.setTextColor(Color.BLACK);
    }
}

[编辑]

当然,在你的声明部分,这样做:

EditText edtTest = null;

在你的 onCreate 中做这样的事情(在 super 和 setContentVieew 行之后):

edtTest = (EditText) findViewById(R.id.test);

【讨论】:

  • 好吧,也许我明白了!最后一件事:我与那个editText1有什么关系?它应该是我的文本视图,那么我该如何链接这些?
  • 对不起,我还是新手!嗯。所以,我在布局中有那个 EditText,在 MainActivity 中有那个 editText1。我的意思是,它们不是同一个对象吗?因为当我复制您之前用 Java 编写的代码时,它给了我 editText1 作为错误:|很抱歉很无聊!
  • 是的,它们是...这取决于您如何命名它们... edtTest 或editText1,或其他:名称必须与我相同。我刚刚添加了我的 EDIT,其名称与示例不同 - 只是因为匆忙(同时做其他 12 件事)。我将编辑我的示例以匹配 edtTest(我喜欢这个伪匈牙利命名)​​ - 好的,完成。
  • 我可以向投反对票的人询问投反对票的原因吗?如果答案已被接受,我认为它不值得 -1。
【解决方案2】:

你的意思是这样的? -

button1.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            editText1.setTextColor(Color.GREEN);
                        }
                    }););

【讨论】:

  • 对不起,我对这个很陌生:S 我必须导入 button1 吗?或者我只需要用我的按钮 ID 替换它? editText1 也一样吗?谢谢! :))
  • 您必须将 button1 和 editText1 替换为您的项目名称...但首先您必须使用如下代码将对视图的引用分配给它们: Button greenBtn = (Button) findViewById(R. id.green_button);
猜你喜欢
  • 2021-05-21
  • 1970-01-01
  • 2016-12-11
  • 2016-04-05
  • 2014-04-04
  • 1970-01-01
  • 2022-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多