2016-05-16 16 views
1

在我的主要活動中,當我點擊時,我有一個登錄按鈕,它改變了此按鈕的背景顏色並將我帶入SignIn活動。返回活動時清除背景行爲

// Button SignInActivity 
    final Button signIn = (Button) findViewById(R.id.btn_sign_in); 
    signIn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      signIn.setBackgroundColor(getResources().getColor(R.color.colorAccent)); 
      Intent intent = new Intent(MainActivity.this, SignInActivity.class); 
      startActivity(intent); 
     } 
    }); 

我的問題是當我回到MainActivity時,我發現SignIn按鈕與colorAccent背景。 任何解決方案來清除背景顏色?

回答

0

你可以這樣做。

首先找到這樣

Drawable d = button.getBackground(); 

按鈕的默認背景,如果你需要默認的背景再次使用這個

button.setBackgroundDrawable(d);您的onResume()方法。

使用這些行管理您的代碼。

+0

感謝它的工作) –

+0

我的榮幸......請將我的回答標記爲已接受。快樂編碼! :) –

+1

當然,完成! –

0

您可以覆蓋你的'的onResume()方法,你需要「你的按鈕,默認顏色的setbackgroundColor」。

@Override 
    public protected onResume(){ 
      signIn.setBackgroundColor(getResources().getColor(R.color.yourcolor)); 
    } 
+0

感謝您的回覆 –

0

您可以創建selector並將其分配給xml中的按鈕。例如: -

在可繪製文件夾內創建一個名爲​​的文件,並複製粘貼下面的選擇器代碼。

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

     <solid android:color="@color/buttonBackgroundPressed"/> 
     <stroke android:width="1dp" android:color="@color/buttonBorderColor"/> 
    </shape> 
</item> 
<item android:state_focused="true"> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 

     <solid android:color="@color/buttonBackgroundPressed"/> 
     <stroke android:width="1dp" android:color="@color/buttonBorderColor"/> 
    </shape> 
</item> 
<item> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 

     <solid android:color="@color/buttonBackgroundNormal"/> 
     <stroke android:width="1dp" android:color="@color/buttonBorderColor"/> 
    </shape> 
</item> 

提供colors.xml

色彩參考然後繪製分配給這樣的按鈕:

<Button 
android:id="@+id/button" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:background="@drawable/button_background" 
android:textColor="@color/colorPrimary" 
android:textSize="@dimen/text_size_16"/> 
0

在繪製文件夾中創建一個自定義按鈕的背景繪製如下

button_background.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
     android:drawable="@android:color/darker_gray" 
     android:state_pressed="true"/> 
    <item 
     android:drawable="@android:color/holo_blue_light" 
     /> 
</selector> 

以此爲背景,用於在XML按鈕,如下

<Button 
android:id="@+id/button" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:background="@drawable/button_background" 
/> 

代碼如下

final Button signIn = (Button) findViewById(R.id.btn_sign_in); 
    signIn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //signIn.setBackgroundColor(getResources().getColor(R.color.colorAccent)); 
      Intent intent = new Intent(MainActivity.this, SignInActivity.class); 
      startActivity(intent); 
     } 
    });