如何在按钮单击时更改textView文本颜色

时间:2016-10-07 16:05:52

标签: android button text

我有一个带有文字的按钮。如何在按钮单击时更改textview中文本的颜色?是否需要在选择器中添加?还是在java代码中?

这是选择器:

<?xml version="1.0" encoding="utf-8"?>

<item android:state_pressed="false">
    <shape android:shape="oval">
        <solid
            android:color="@color/blue_800"/>
    </shape>
</item>
<item android:state_pressed="true">
    <shape android:shape="oval">
        <solid android:color="@color/blue_300"/>
    </shape>
</item>

到目前为止的布局:

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<Button
    android:id="@+id/imageUploader1"
    android:background="@drawable/round_button"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginRight="2dp"
    android:layout_marginLeft="2dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Main"
    android:layout_gravity="center"/>
</LinearLayout>

2 个答案:

答案 0 :(得分:2)

只需在您的java代码

中应用它
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        textView.setTextColor(Color.BLUE);
    }
});

答案 1 :(得分:0)

    //here is a TextView and two Buttons with different color
<TextView
    android:id="@+id/txt"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:text="Hello World"
    android:gravity="center"
    android:textSize="24sp"
    android:fontFamily="sans-serif-black"
    android:letterSpacing="0.03"
    android:layout_marginTop="30dp"
    />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <Button
        android:id="@+id/red"
        android:layout_width="150dp"
        android:layout_height="80dp"
        android:text="Red"
        android:fontFamily="sans-serif-black"
        android:textSize="22sp"
        android:backgroundTint="@color/Red"
        android:textColor="@color/white"
        />

    <Button
        android:id="@+id/green"
        android:layout_width="150dp"
        android:layout_height="80dp"
        android:text="Red"
        android:fontFamily="sans-serif-black"
        android:textSize="22sp"
        android:backgroundTint="@color/Green"
        android:textColor="@color/white"
        />

</LinearLayout>


//here is a java code
public class MainActivity extends AppCompatActivity {
TextView text;
Button red, green;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text = (TextView)findViewById(R.id.txt);
    red = (Button)findViewById(R.id.red);
    green = (Button)findViewById(R.id.green);

    red.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            text.setTextColor(Color.parseColor("#FF0000"));
        }
    });
    green.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            text.setTextColor(Color.parseColor("#04AF0A"));
        }
    });
}