2016-05-03 3 views
0

У меня есть два TextViews, расположенных бок о бок горизонтально, и они имеют прямоугольную форму с закругленными углами.Изменяемый фон из двух текстовых переменных alternativly

Я хотел бы:

  1. изменить цвет фона, который по умолчанию ни один в белый при нажатии или нажатии и обратно умолчанию когда второй TextView является щелкнул или нажатие,
  2. прокладка, которая находится между формой и текстом, должна быть белый при нажатии и bac K к умолчанию когда другой выбран
  3. TextColor идти черный, когда фон идет белый

Xml код из двух TextViews:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_gravity="center" 
    android:layout_marginTop="5dp" 
    android:background="@drawable/toggle" 
    android:id="@+id/l1" 
    > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Coach" 
     android:paddingLeft="5dp" 
     android:textColor="#ffffff" 
     android:id="@+id/TV" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginRight="10dp" 
    /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Client" 
     android:paddingRight="5dp" 
     android:textColor="#ffffff" 
     android:id="@+id/TV1" 
     android:layout_gravity="center_horizontal" 
    /> 

</LinearLayout> 

_____ Java Code ____________

package com.example.jhang.sample; 

import android.app.Activity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

import static android.R.color.black; 
import static android.R.color.white; 

/** 
* Created by jhang on 5/2/2016. 
*/ 
public class Activity1 extends Activity implements View.OnClickListener { 

TextView coach, client, about, privacy, faq, contact,login; 
LinearLayout l1; 
int count=0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity1); 

    coach=(TextView)findViewById(R.id.TV); 
    client=(TextView)findViewById(R.id.TV1); 
    about=(TextView)findViewById(R.id.TV2); 
    privacy=(TextView)findViewById(R.id.TV3); 
    faq=(TextView)findViewById(R.id.TV4); 
    contact=(TextView)findViewById(R.id.TV5); 
    login=(TextView)findViewById(R.id.button); 
    l1=(LinearLayout)findViewById(R.id.l1); 

    coach.setOnClickListener(this); 
    client.setOnClickListener(this); 
    about.setOnClickListener(this); 
    privacy.setOnClickListener(this); 
    faq.setOnClickListener(this); 
    contact.setOnClickListener(this); 
    login.setOnClickListener(this); 
    l1.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) 
{ 


    if (count==0) 
    { 
     coach.setBackgroundColor(Color.WHITE); 
     coach.setTextColor(Color.BLACK); 
     count=1; 
    } 
    else if (count==1) 
    { 
     client.setBackgroundColor(Color.WHITE); 
     client.setTextColor(Color.BLACK); 
     count=0; 
    } 

} 


} 
+0

Показать, что вы делали до сих пор –

+0

вы можете показать свой код Java ? –

ответ

1
public void onClick(View v) 
{ 


if (count==0) 
{ 

    client.setBackgroundColor(Color.BLACK); 
    client.setTextColor(Color.WHITE); 
    coach.setBackgroundColor(Color.WHITE); 
    coach.setTextColor(Color.BLACK); 
    count=1; 
} 
    else if (count==1) 
{ 

    coach.setBackgroundColor(Color.BLACK); 
    coach.setTextColor(Color.White); 

    client.setBackgroundColor(Color.WHITE); 
    client.setTextColor(Color.BLACK); 
    count=0; 
} 

} 
+0

Спасибо, что отработал –

0

Попробуйте этот путь, создать этот XML button_press

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 

    <solid android:color="@color/blue" /> 

    <stroke 
     android:width="4dp" 
     android:color="@color/blue" /> 

    <padding 
     android:bottom="1dp" 
     android:left="1dp" 
     android:right="1dp" 
     android:top="1dp" /> 

</shape> 

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="12dp" 
     android:background="@drawable/button_press" 
     android:onClick="onClick" 
     android:text="Press 1" /> 

    <TextView 
     android:id="@+id/btn2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="12dp" 
     android:background="@drawable/button_press" 
     android:onClick="onClick" 
     android:text="Press 2" /> 

</LinearLayout> 

Java

Textview button; 

public void onClick(View v) { 

    Drawable dr = getResources().getDrawable(R.drawable.button_press); 
    dr.setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC_ATOP); 

    switch (v.getId()) { 
    case R.id.btn: 

     if (button == null) { 
      button = (TextView) findViewById(v.getId()); 
     } else { 
      button.setBackgroundResource(R.drawable.button_press); 
      button = (TextView) findViewById(v.getId()); 
     } 
     button.setBackgroundDrawable(dr); 

     break; 

    case R.id.btn2: 
     if (button == null) { 
      button = (TextView) findViewById(v.getId()); 
     } else { 
      button.setBackgroundResource(R.drawable.button_press); 
      button = (TextView) findViewById(v.getId()); 
     } 
     button.setBackgroundDrawable(dr); 

     break; 

    default: 
     break; 
    } 
}