android有两个按钮,一个总是被点击

时间:2016-05-12 10:16:28

标签: android xml android-layout android-studio button

如何在每个按钮旁边制作两个按钮,一个按钮默认为单击,按下按钮时另一个按钮未按下?

由于

1 个答案:

答案 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));
        }
    }