2016-05-03 3 views
0

Ich habe zwei TextViews horizontal nebeneinander angeordnet und sie sind in einem Rechteck mit abgerundeten Ecken.Hintergrund von zwei TextView alternativ ändern

Ich möchte:

  1. den Hintergrund ändern, die standardmäßig keine-weiß wenn gedrückt oder angeklickt und zurück zu Standard, wenn der zweite Textview ist geklickt oder gepresst,
  2. die Polsterung, die zwischen der Form und dem Text ist weiß wenn gedrückt und bac k zu Standard, wenn die anderen
  3. die Textfarbe ausgewählt ist schwarz zu gehen, wenn der Hintergrund wissen

XML-Code der beiden Textviews geht:

<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

Zeigen Sie, was Sie getan haben weit –

+0

können Sie Ihr Java-Code anzeigen ? –

Antwort

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

Danke payal, das hat funktioniert –

0

auf diese Weise versuchen, erstellen Sie diese 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; 
    } 
} 
Verwandte Themen