我創建了一個線性佈局,其中有多個文本視圖。目前,我一次只能突出顯示一個文本視圖。現在我想要每當用戶點擊任何文本視圖時,它應該突出顯示,並且我想限制用戶最多3個選擇。單擊突出顯示textview
這裏是我的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));
}
這裏是我繪製的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>
任何人都可以建議我怎麼能我達到了嗎?
使用選擇器爲 –
您可以使用計數器來實現此目的。 –
@VishalPatoliya我已經使用選擇器 –