Ich habe ein lineares Layout erstellt, in dem ich mehrere Textansicht habe. Zurzeit kann ich jeweils nur eine Textansicht markieren. Jetzt will ich Immer wenn ein Benutzer auf eine Textansicht klickt, sollte er hervorgehoben werden und ich möchte den Benutzer auf maximal 3 Auswahl beschränken.Markieren Sie Textansicht auf klicken
Hier ist meine XML-Datei:
<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>
Hier ist meine Klasse-Datei:
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));
}
Hier ist meine ziehbar XML-Datei:
<?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>
Kann mir jemand vorschlagen kann, wie Ich erreiche es?
Verwenden Sie Selektor für diese –
Sie Zähler verwenden können, um dies zu erreichen. –
@VishalPatoliya Ich habe bereits Selektor verwenden –