android 如何更改textView按钮点击时的文本颜色

xe55xuns  于 2023-01-28  发布在  Android
关注(0)|答案(2)|浏览(195)

我有一个按钮,下面有一个文本。我如何改变按钮点击时文本视图中的文本颜色?它需要添加到选择器中吗?还是在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>
neskvpey

neskvpey1#

只需在java代码中应用它

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

kxkpmulp2#

//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"));
            }
        });
    }

相关问题