gpt4 book ai didi

java - 我使用 Onclick 按钮事件来更改颜色,但当应用程序进入横向模式时,它会丢失颜色。该怎么办?

转载 作者:太空宇宙 更新时间:2023-11-04 13:40:18 24 4
gpt4 key购买 nike

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light"
>


<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_marginTop="31dp"
android:text="What do u Think about the Android?"
android:textSize="20dp" />

<Button
android:id="@+id/btn01"
android:layout_width="156dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="31dp"
android:text="Site" />

<Button
android:id="@+id/btn02"
android:layout_width="156dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn01"
android:layout_alignRight="@+id/btn01"
android:layout_below="@+id/btn01"
android:layout_marginTop="25dp"
android:text="Mobile" />

<Button
android:id="@+id/btn03"
android:layout_width="156dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn02"
android:layout_alignRight="@+id/btn02"
android:layout_below="@+id/btn02"
android:layout_marginTop="25dp"
android:text="Os" />

<Button
android:id="@+id/btn04"
android:layout_width="156dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn03"
android:layout_alignRight="@+id/btn03"
android:layout_below="@+id/btn03"
android:layout_marginTop="25dp"
android:text="Environment" />

</RelativeLayout>

我的 Xml 文件有四个按钮和一个 TextView

private Button btn1 ,btn2,btn3,btn4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mains);


btn1 = (Button) findViewById(R.id.btn01);
btn2 = (Button)findViewById(R.id.btn02);
btn3 = (Button)findViewById(R.id.btn03);
btn4 = (Button) findViewById(R.id.btn04);


btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);

}
@Override
public void onClick(View v) {
if(v.getId() == R.id.btn01) {
btn1.setBackgroundColor(Color.RED);
btn1.setTextColor(Color.WHITE);

}
if(v.getId() == R.id.btn02) {

btn2.setBackgroundColor(Color.RED);
btn2.setTextColor(Color.WHITE);

}
if(v.getId() == R.id.btn03) {
btn3.setBackgroundColor(Color.GREEN);
btn3.setTextColor(Color.WHITE);

}

if(v.getId() == R.id.btn04) {

btn4.setBackgroundColor(Color.RED);
btn4.setTextColor(Color.WHITE);
}

}

}

包含按钮颜色所有代码的java文件。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mynew"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />


<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name="com.example.mynew.MainActivity"
android:configChanges="orientation"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>

</application>

list 包含想要的一切。请帮我解决这个问题。该应用程序在纵向模式下工作正常,但所选按钮在横向模式下失去颜色

最佳答案

您可以将 android:freezesText="true" 设置为您的按钮。或者,您可以在 onSaveInstanceState() 中手动保存每个按钮的状态,然后当您在 onCreate()onRestoreInstanceState() 回调中获取该值时。

例如(假设您只有两个按钮):

private boolean btn1Pressed = false;
private boolean btn2Pressed = false;

static final String STATE_BUTTON1 = "button_1";
static final String STATE_BUTTON2 = "button_2";

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the buttons current state
savedInstanceState.putBoolean(STATE_BUTTON1, btn1Pressed);
savedInstanceState.putBoolean(STATE_BUTTON2, btn2Pressed);

super.onSaveInstanceState(savedInstanceState);
}

以及来自 onCreate()onRestoreInstanceState() :

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

if (savedInstanceState != null) {
// Restore states of buttons from saved state
btn1Pressed = savedInstanceState.getBoolean(STATE_BUTTON1, false);
btn2Pressed = savedInstanceState.getBoolean(STATE_BUTTON2, false);
}

btn1 = (Button)findViewById(R.id.btn01);
btn1.setOnClickListener(this);
btn2 = (Button)findViewById(R.id.btn02);
btn2.setOnClickListener(this);

if(btn1Pressed){
changeColor(btn1);
}

if(btn2Pressed){
changeColor(btn2);
}
}

private void changeColor(Button b){
b.setBackgroundColor(Color.RED);
b.setTextColor(Color.WHITE);
}

此外,在您的 onClick() 方法中,如果按下按钮,您需要设置新状态。例如:

if(v.getId() == R.id.btn01) {
btn1Pressed = true;
changeColor((Button)v);
}
// do the same with other buttons...

关于java - 我使用 Onclick 按钮事件来更改颜色,但当应用程序进入横向模式时,它会丢失颜色。该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31296812/

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