我是Android Studio的全新產品,並且正試圖弄清楚如何更改我的啓動應用程序的背景顏色。Android Studio多次改變背景顏色,點擊1次按鈕
應用程序加載的那一刻,我在屏幕上看到一個按鈕,當我點擊時,它會變成紅色。
我想要的是當你點擊按鈕時,它會從紅色變成綠色變成藍色變成紅色。
不過,我不斷收到這些錯誤:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'. Compilation failed; see the compiler error output for details. Error:(72, 9) error: class, interface, or enum expected
主要活動XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/layout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Color"
android:onClick="onChangeColor"/>
</LinearLayout>
試驗用Java代碼:
private int colorIndex = 1;
public void onChangeColor(View view) {
int color;
if(colorIndex==0) {
color = Color.RED;
colorIndex = 1;
}else if(colorIndex==1) {
color = Color.GREEN;
colorIndex = 2;
}else {
//colorIndex = 2
color = Color.BLUE;
colorIndex = 0;
}
View layout = findViewById(R.id.layout);
layout.setBackgroundColor(color);
}
public class TestActivity extends AppCompatActivity {
View view;
//declare a string variable in java a class
//private var colour = "green";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View layout = findViewById(R.id.layout);
layout.setBackgroundColor(Color.RED);
view= this.getWindow().getDecorView();
view.setBackgroundResource(R.color.gray);
}
public void goRed(View v)
{
//if (colour == "green"){
view.setBackgroundResource(R.color.red);
//colour = "red";
//}
}
}
您應該添加你的代碼! –
有我的代碼頭腦公共類主要活動擴展appcompatactivity和視圖視圖。希望你或其他人能幫助我,所以我可以在顏色之間切換。 –
'應用程序加載的那一刻,我在屏幕上看到一個按鈕,當我點擊它時會轉到紅色'你看到任何按鈕?!男人,那是你真正的代碼嗎? –