2016-05-12 62 views
-2

如何在每個按鈕旁邊放置兩個按鈕,其中一個按鈕默認爲單擊並且每按一次按鈕,另一個按鈕未按下?安卓系統有兩個按鈕,其中一個總是點擊

感謝

+2

http://stackoverflow.com/help/how-to-ask –

+0

聽起來像一個單選按鈕' ' – 0xDEADC0DE

+0

我看着單選按鈕,但我正在尋找正常按鈕的外觀 –

回答

0

檢查佈局現在

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/line_bet" 
    android:layout_width="match_parent" 
    android:layout_height="50dip" 
    android:baselineAligned="false" 
    android:orientation="horizontal" 
    android:weightSum="2" > 

    <LinearLayout 
     android:layout_width="0dip" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:padding="3dip" > 

     <Button 
      android:id="@+id/txtWork" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#607d8b" 
      android:gravity="center|start" 
      android:singleLine="true" 
      android:text="" 
      android:textColor="@color/white" 
      android:textSize="@dimen/font_normal_size" /> 
    </LinearLayout > 

    <LinearLayout 
     android:layout_width="0dip" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:padding="3dip" > 

     <Button 
      android:id="@+id/txtNot" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@color/white" 
      android:gravity="center|start" 
      android:singleLine="true" 
      android:text="" 
      android:textColor="@color/colorAccent" 
      android:textSize="@dimen/font_normal_size" /> 
    </LinearLayout > 
</LinearLayout > 

,這樣做

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout_name); 

     SetButtonChecked(true); 
    } 

private void SetButtonChecked(boolean isSelected) { 
     if (isSelected) { 
      txtWork.setBackgroundColor(ContextCompat.getColor(YourActvityName.class, R.color.colorAccent)); 
      txtWork.setTextColor(ContextCompat.getColor(getActivity(), R.color.white)); 
     } else { 
      txtWork.setBackgroundColor(ContextCompat.getColor(YourActvityName.class, R.color.white)); 
      txtWork.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorAccent)); 
     } 
    }