Android Studio多次改变背景颜色,点击1次按钮
我是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";
//}
}
}
给你一个优秀的帮助,有必要看看你的代码。 任何方式,如果我andersted你的权利,也许这将帮助你:
在你的XML布局:
<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>
你的活动:
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);
}
在你onCreate
在活动
View layout = findViewById(R.id.layout);
layout.setBackgroundColor(Color.RED);
我仍然有一些问题和问题。 –
你还没有用你的按钮向我们展示你的代码。 – csmckelvey
那就是我在我的Activity_Main xml文件和我的TestActivity.java文件中的所有代码。 –
如果我正确理解你想要的是转换随着时间推移一系列颜色,每种颜色持续1-2秒。您可以使用Android的默认CountDownTimer。
保持您的xml布局相同。
在你的活动:
public class TestActivity extends AppCompatActivity {
LinearLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (LinearLayout)findViewById(R.id.layout);
layout.setBackgroundColor(Color.RED);
}
public void onChangeColor(View view) {
// start your timer on button click
new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
changeBackground(3-millisUntilFinished/1000);
}
}.start();
}
private void changeBackground(int colorIndex){
int color;
if(colorIndex==1) {
color = Color.GREEN;
}else if(colorIndex==2) {
color = Color.BLUE;
}else {
color = Color.RED;
}
layout.setBackgroundColor(color);
}
}
希望这可以帮助。如果我误解了某些内容,请发表评论。
编辑:我类型强制转换观的LinearLayout
这正是我想要的,并希望在最后一个颜色后回到开始 –
我也试图运行它,但我得到这个错误错误运行TestActivity:com.example.bikam.mycolorapp。TestActivity不是一个Activity子类或别名 –
我已经做了一些更改,如果这不能解决,您能否请您发布准确的Activity代码和您遇到错误的行号。 – deepankar
您应该添加你的代码! –
有我的代码头脑公共类主要活动扩展appcompatactivity和视图视图。希望你或其他人能帮助我,所以我可以在颜色之间切换。 –
'应用程序加载的那一刻,我在屏幕上看到一个按钮,当我点击它时会转到红色'你看到任何按钮?!男人,那是你真正的代码吗? –