点击时突出显示textview

时间:2016-09-08 06:24:16

标签: android

我创建了一个线性布局,其中我有多个文本视图。目前,我一次只能突出显示一个文本视图。现在我想要每当用户点击任何文本视图时,它应该突出显示,并且我想限制用户最多3个选择。

这是我的XML文件:

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/goal"
    android:layout_marginTop="15dp"
    android:orientation="vertical">

        <TextView
            android:id="@+id/goalText1"
            android:layout_width="match_parent"
            android:layout_height="63dp"
            android:background="@drawable/white_border_rounded"
            android:padding="4dp"
            android:layout_margin="18dp"
            android:layout_gravity="fill"
            android:gravity="center"
            android:text="@string/goal1"
            android:textColor="@color/white"
            android:textSize="21sp"/>
            <TextView
                android:id="@+id/goalText2"
                android:layout_width="match_parent"
                android:layout_height="63dp"
                android:background="@drawable/white_border_rounded"
                android:padding="4dp"
                android:layout_margin="18dp"
                android:layout_gravity="fill"
                android:gravity="center"
                android:text="@string/goal2"
                android:textColor="@color/white"
                android:textSize="21sp"/>
            <TextView
                android:id="@+id/goalText3"
                android:layout_width="match_parent"
                android:layout_height="63dp"
                android:background="@drawable/white_border_rounded"
                android:padding="4dp"
                android:layout_margin="18dp"
                android:layout_gravity="fill"
                android:gravity="center"
                android:text="@string/goal3"
                android:textColor="@color/white"
                android:textSize="21sp"/>
            <TextView
                android:id="@+id/goalText4"
                android:layout_width="match_parent"
                android:layout_height="63dp"
                android:background="@drawable/white_border_rounded"
                android:padding="4dp"
                android:layout_margin="18dp"
                android:layout_gravity="fill"
                android:gravity="center"
                android:text="@string/goal4"
                android:textColor="@color/white"
                android:textSize="21sp"/>

            <TextView
                android:id="@+id/goalText5"
                android:layout_width="match_parent"
                android:layout_height="63dp"
                android:background="@drawable/white_border_rounded"
                android:padding="4dp"
                android:layout_margin="18dp"
                android:layout_gravity="fill"
                android:gravity="center"
                android:text="@string/goal5"
                android:textColor="@color/white"
                android:textSize="21sp"/>
            <TextView
                android:id="@+id/goalText6"
                android:layout_width="match_parent"
                android:layout_height="63dp"
                android:background="@drawable/white_border_rounded"
                android:padding="4dp"
                android:layout_margin="18dp"
                android:layout_gravity="fill"
                android:gravity="center"
                android:text="@string/goal6"
                android:textColor="@color/white"
                android:textSize="21sp"/>
            <TextView
                android:id="@+id/goalText7"
                android:layout_width="match_parent"
                android:layout_height="63dp"
                android:background="@drawable/white_border_rounded"
                android:padding="4dp"
                android:layout_margin="18dp"
                android:layout_gravity="fill"
                android:gravity="center"
                android:text="@string/goal7"
                android:textColor="@color/white"
                android:textSize="21sp"/>
            <TextView
                android:id="@+id/goalText8"
                android:layout_width="match_parent"
                android:layout_height="63dp"
                android:background="@drawable/white_border_rounded"
                android:padding="4dp"
                android:layout_margin="18dp"
                android:layout_gravity="fill"
                android:gravity="center"
                android:text="@string/goal8"
                android:textColor="@color/white"
                android:textSize="21sp"/>
</LinearLayout>

这是我的班级文件:

 public void onClick(View v) {
    switch (v.getId()) {
        case R.id.goalText1:
            changeViewBackground(true, false, false, false);
            goal_selection = mGoal1.getText().toString();
            break;

        case R.id.goalText2:
            changeViewBackground(false, true, false, false);
            goal_selection = mGoal2.getText().toString();
            break;

        case R.id.goalText3:
            changeViewBackground(false, false, true, false);
            goal_selection = mGoal3.getText().toString();
            break;

        case R.id.goalText4:
            changeViewBackground(false, false, false, true);
            goal_selection = mGoal4.getText().toString();
            break;

        case R.id.btnGoal:
            Intent intent = new Intent(this, fiteness_level_selection.class);


            try {
                obj.put("SelectedGoal", goal_selection);
            } catch (Exception e) {
                e.printStackTrace();
            }
            intent.putExtra("GoalJson", obj.toString());
            startActivity(intent);
            break;


    }

}

private void changeViewBackground(boolean view1, boolean view2, boolean
        view3, boolean view4) {
    mGoal1.setSelected(view1);
    mGoal2.setSelected(view2);
    mGoal3.setSelected(view3);
    mGoal4.setSelected(view4);
    if(view1==true){
        mGoal1.setTextColor(getResources().getColor(R.color.black));
    }
    else {
        mGoal1.setTextColor(getResources().getColor(R.color.white));
    }
    if(view2==true){
        mGoal2.setTextColor(getResources().getColor(R.color.black));
    }
    else {
        mGoal2.setTextColor(getResources().getColor(R.color.white));
    }
    if(view3==true){
        mGoal3.setTextColor(getResources().getColor(R.color.black));
    }
    else {
        mGoal3.setTextColor(getResources().getColor(R.color.white));
    }
    if(view4==true){
        mGoal4.setTextColor(getResources().getColor(R.color.black));
    }
    else {
        mGoal4.setTextColor(getResources().getColor(R.color.white));
    }

这是我的drawable xml文件:

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="false" android:state_selected="false" >
        <shape android:shape="rectangle"  >
            <corners android:radius="50dip" />
            <stroke android:width="1dip" android:color="@color/white" />
        </shape>

    </item>
    <item android:state_pressed="true" android:state_selected="false" >
        <shape android:shape="rectangle"  >
            <corners android:radius="50dip" />
            <stroke android:width="1dip" android:color="@color/white" />
            <solid android:color="#fff"/>
        </shape>
    </item>
    <item android:state_pressed="false" android:state_selected="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="50dip" />
            <stroke android:width="1dip" android:color="@color/white" />
            <solid android:color="#fff"/>
        </shape>
    </item>
    <item >
        <shape android:state_pressed="true" android:state_selected="true" >
            <corners android:radius="50dip" />
            <stroke android:width="1dip" android:color="@color/white" />
            <solid android:color="#fff"/>
        </shape>
    </item>

</selector>

任何人都可以建议我如何实现它?

2 个答案:

答案 0 :(得分:0)

尝试将changeViewBackground方法更改为以下,

private int count=0;
private void changeViewBackground(TextView tv){
count++;

if(count<=3){
tv.setTextColor(getResources().getColor(R.color.black));
tv.setSelected(true);
}else{
count=0;
tv.setTextColor(getResources().getColor(R.color.white));
tv.setSelected(false);
}

}

将需要突出显示的textview传递给上面的方法。

答案 1 :(得分:0)

试试这段代码......

  @Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.goalText1:
            if (count <= 2) {
                mGoal1.setTextColor(getResources().getColor(R.color.black));
                count++;
            } else {
                Toast.makeText(Main2Activity.this, "cant select more", Toast.LENGTH_SHORT).show();
            }
            break;

        case R.id.goalText2:
            if (count <= 2) {
                mGoal2.setTextColor(getResources().getColor(R.color.black));
                count++;
            } else {
                Toast.makeText(Main2Activity.this, "cant select more", Toast.LENGTH_SHORT).show();
            }
            break;

        case R.id.goalText3:
            if (count <= 2) {
                mGoal3.setTextColor(getResources().getColor(R.color.black));
                count++;
            } else {
                Toast.makeText(Main2Activity.this, "cant select more", Toast.LENGTH_SHORT).show();
            }
            break;

        case R.id.goalText4:
            if (count <= 2) {
                mGoal4.setTextColor(getResources().getColor(R.color.black));
                count++;
            } else {
                Toast.makeText(Main2Activity.this, "cant select more", Toast.LENGTH_SHORT).show();
            }
            break;
    }
}

使用此功能,您只能选择3个文本视图,但如果您还必须取消选择文本视图,则必须(count - ;)在那里。