gpt4 book ai didi

java - 如何更改未单击的按钮的背景颜色?

转载 作者:行者123 更新时间:2023-12-02 12:12:13 24 4
gpt4 key购买 nike

我的目标是每次单击按钮时,Button 背景颜色都会恢复为默认颜色,并且单击的 Button 会更改颜色。我希望在 JAVA 中执行此操作,我认为 for 循环是可行的方法,但我不确定如何编辑未单击的按钮。

这是我的 XML

    <Button
android:id="@+id/but1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:background="@drawable/background_selector"
android:onClick="buttonOn"
android:text="Button 1" />
<Button
android:id="@+id/but2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:background="@drawable/background_selector"
android:onClick="buttonOn"
android:text="Button 2" />
<Button
android:id="@+id/but3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:background="@drawable/background_selector"
android:onClick="buttonOn"
android:text="Button 3" />

这是我的选择器

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/background_blue"
android:state_pressed="true"/>

<item android:drawable="@drawable/background_blue"
android:state_focused="true"/>

<item android:drawable="@drawable/background_white"/>
</selector>

这里是background_white

<solid android:color="@android:color/white" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>

这是背景_蓝色

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@android:color/holo_blue_bright" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>

这是我的java代码

public class MainActivity extends AppCompatActivity {
Button but1;
Button but2;
Button but3;
Button but4;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

but1 = (Button) findViewById(R.id.but1);
but2 = (Button) findViewById(R.id.but2);
but3 = (Button) findViewById(R.id.but3);
but4 = (Button) findViewById(R.id.but4);
}

public void buttonOn(View v) {
Fragment view;
switch(v.getId()) {
case R.id.but1:
Log.i("Button 1", "pressed");
break;
case R.id.but2:
Log.i("Button 2", "pressed");
break;
case R.id.but3:
Log.i("Button 3", "pressed");
break;
case R.id.but4:
Log.i("Button 4", "pressed");
break;
}
}

最佳答案

将背景作为可绘制选择器添加到按钮。还添加 android:focusableInTouchMode="true"以使按钮在按下时进行触摸。

<Button
android:id="@+id/but1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:background="@drawable/background_selector"
android:onClick="buttonOn"
android:text="Button 1" />

然后为background_selector内选中和未选中的按钮添加两个可绘制对象

background_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/background_blue"
android:state_pressed="true"/>

<item android:drawable="@drawable/background_blue"
android:state_focused="true"/>

<item android:drawable="@drawable/background_white"/>
</selector>

然后将background_white和background_blue添加到drawable文件夹

background_white.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="@android:color/white" />

<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>

background_blue.xml

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="@android:color/holo_blue_bright" />

<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>

关于java - 如何更改未单击的按钮的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46461571/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com