2017-02-21 364 views
-4

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

} 
} 
+1

您應該添加你的代碼! –

+0

有我的代碼頭腦公共類主要活動擴展appcompatactivity和視圖視圖。希望你或其他人能幫助我,所以我可以在顏色之間切換。 –

+1

'應用程序加載的那一刻,我在屏幕上看到一個按鈕,當我點擊它時會轉到紅色'你看到任何按鈕?!男人,那是你真正的代碼嗎? –

回答

0

給你一個優秀的幫助,有必要看看你的代碼。 任何方式,如果我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); 
+0

我仍然有一些問題和問題。 –

+0

你還沒有用你的按鈕向我們展示你的代碼。 – csmckelvey

+0

那就是我在我的Activity_Main xml文件和我的TestActivity.java文件中的所有代碼。 –

0

如果我正確理解你想要的是轉換隨着時間推移一系列顏色,每種顏色持續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

+0

這正是我想要的,並希望在最後一個顏色後回到開始 –

+0

我也試圖運行它,但我得到這個錯誤錯誤運行TestActivity:com.example.bikam.mycolorapp。TestActivity不是一個Activity子類或別名 –

+0

我已經做了一些更改,如果這不能解決,您能否請您發佈準確的Activity代碼和您遇到錯誤的行號。 – deepankar