2016-03-16 36 views
1

我希望Java文件中的代碼能夠設置任何按鈕被點擊的背景顏色,並保留它,即使您放開按鈕後也是如此。在此先感謝用於手風琴菜單的Android背景顏色

我真的沒有在這裏格式化。我希望很清楚什麼屬於哪裏。

的Java:

package mika.actual; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class accordion extends Activity { 
    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.accordian); 

     Button btnProfile = (Button) findViewById(R.id.btnProfile); 
     Button btnSettings = (Button) findViewById(R.id.btnSettings); 
     Button btnPrivacy = (Button) findViewById(R.id.btnPrivacy); 

     View panelProfile = findViewById(R.id.panelProfile); 
     panelProfile.setVisibility(View.GONE); 

     View panelSettings = findViewById(R.id.panelSettings); 
     panelSettings.setVisibility(View.GONE); 

     View panelPrivacy = findViewById(R.id.panelPrivacy); 
     panelPrivacy.setVisibility(View.GONE); 

     btnProfile.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // DO STUFF 
       View panelProfile = findViewById(R.id.panelProfile); 
       panelProfile.setVisibility(View.VISIBLE); 

       View panelSettings = findViewById(R.id.panelSettings); 
       panelSettings.setVisibility(View.GONE); 

       View panelPrivacy = findViewById(R.id.panelPrivacy); 
       panelPrivacy.setVisibility(View.GONE); 

      } 
     }); 

     btnSettings.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // DO STUFF 
       View panelProfile = findViewById(R.id.panelProfile); 
       panelProfile.setVisibility(View.GONE); 

       View panelSettings = findViewById(R.id.panelSettings); 
       panelSettings.setVisibility(View.VISIBLE); 

       View panelPrivacy = findViewById(R.id.panelPrivacy); 
       panelPrivacy.setVisibility(View.GONE); 

      } 
     }); 

     btnPrivacy.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // DO STUFF 
       View panelProfile = findViewById(R.id.panelProfile); 
       panelProfile.setVisibility(View.GONE); 

       View panelSettings = findViewById(R.id.panelSettings); 
       panelSettings.setVisibility(View.GONE); 

       View panelPrivacy = findViewById(R.id.panelPrivacy); 
       panelPrivacy.setVisibility(View.VISIBLE); 

      } 
     }); 

    } 

} 

XML:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/root_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFFFFFFF" 
    android:orientation="vertical"> 

    <Button 
     android:id="@+id/btnProfile" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Profile" 
     android:textColor="#FFFFFFFF" /> 

    <LinearLayout 
     android:id="@+id/panelProfile" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="#FFFFFFFF"> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#FFFFFFFF"> 

       <LinearLayout 
        android:id="@+id/panelProfile1" 
        android:orientation="horizontal" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="#FFFFFFFF"> 

        <TextView 
         android:id="@+id/strName" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Name" /> 

        <EditText 
         android:id="@+id/txtName" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" /> 

       </LinearLayout> 


       <LinearLayout 
        android:id="@+id/panelProfile2" 
        android:orientation="horizontal" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="#FFFFFFFF"> 

        <TextView 
         android:id="@+id/strSurname" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Surname" /> 

        <EditText 
         android:id="@+id/txtSurname" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" /> 

       </LinearLayout> 

      </LinearLayout> 

     </ScrollView> 

    </LinearLayout> 



    <Button 
     android:id="@+id/btnSettings" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Settings" 
     android:textColor="#FFFFFFFF" /> 

    <LinearLayout 
     android:id="@+id/panelSettings" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:background="#FFFFFFFF"> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"> 

       <LinearLayout 
        android:id="@+id/panelSettings1" 
        android:orientation="horizontal" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="#FFFFFFFF"> 

        <TextView 
         android:id="@+id/strMail" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="e-mail" /> 

        <EditText 
         android:id="@+id/txtMail" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" /> 

       </LinearLayout> 

       <LinearLayout 
        android:id="@+id/panelSettings2" 
        android:orientation="horizontal" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:background="#FFFFFFFF"> 

        <TextView 
         android:id="@+id/strPhone" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Phone" /> 

        <EditText 
         android:id="@+id/txtPhone" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" /> 

       </LinearLayout> 

      </LinearLayout> 

     </ScrollView> 

    </LinearLayout> 



    <Button 
     android:id="@+id/btnPrivacy" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Privacy" 
     android:textColor="#FFFFFFFF" /> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:id="@+id/panelPrivacy" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="#FFFFFFFF"> 

      <CheckBox 
       android:id="@+id/checkFacebook" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Facebook" 
       android:textColor="#ff355689"> 
      </CheckBox> 
      <CheckBox 
       android:id="@+id/checkLinkedIn" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="LinkedIn" 
       android:textColor="#ff355689"> 
      </CheckBox> 
      <CheckBox 
       android:id="@+id/checkTwitter" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Twitter" 
       android:textColor="#ff355689"> 
      </CheckBox> 

     </LinearLayout> 

    </ScrollView> 

</LinearLayout> 
+0

您是否考慮過使用選擇器? – joao2fast4u

+0

你基本上想要一個選定/未選中狀態,是嗎? – joao2fast4u

回答

0

如果我理解正確的話,你想要做的是,點擊一個按鈕,並擴大其子菜單時,更改背景是什麼直到再次點擊該按鈕。你可以用按鈕上的標籤做到這一點:

btnSettings.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     // DO STUFF 
     //if button is not selected, change background and tag the View as selected 
     if(v.getTag() == null || v.getTag().equals("not_selected")) { 
      v.setBackgroundColor(Color.RED); 
      v.setTag("selected"); 
      //show sub-menu 
     } 
     //if button is already selected, reset background and tag the View as not selected 
     else{ 
      v.setBackgroundResource(android.R.drawable.btn_default); 
      v.setTag("not_selected"); 
      //hide sub menu 
     } 

    } 
}); 

注意v.setBackgroundResource(android.R.drawable.btn_default);正在重置按鈕背景到Android繪製。您可能想要將其更改爲自定義繪圖或顏色。 你可以使用ExpandableListView來實現這種菜單。

+0

謝謝。我對此很新,所以感謝提示。我會研究它。 – lenny

0
button.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       if (event.getAction() == MotionEvent.ACTION_DOWN) { 
        view.setBackgroundColor(ContextCompat.getColor(context, R.color.presed_color)); 
       } else if (event.getAction() == MotionEvent.ACTION_UP) { 
        view.setBackgroundColor(ContextCompat.getColor(context, R.color.unpresed_color)); 
       } 
       return false; 
      } 
     });