【问题标题】:Changing background of two TextView alternativly交替更改两个 TextView 的背景
【发布时间】:2016-05-03 19:15:17
【问题描述】:

我有两个 TextView 水平并排放置,它们是带圆角的矩形。

我想要:

  1. 将默认情况下 none 的背景更改为 white 时 当第二个 TextView 被按下或单击并返回 default 点击或按下,
  2. 形状和文本之间的边距在按下时为白色,当另一个被选中时返回到默认
  3. 当背景变为白色时,Textcolor变为黑色

两个TextView的xml代码:

<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 代码________

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;
    }

}


}

【问题讨论】:

  • 显示你到目前为止所做的事情
  • 你能展示你的java代码吗?

标签: android android-layout


【解决方案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;
}

}

【讨论】:

    【解决方案2】:

    试试这个方法,创建这个 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-19
      • 2021-03-06
      • 1970-01-01
      • 1970-01-01
      • 2011-12-26
      • 2014-06-12
      • 2011-06-05
      • 1970-01-01
      相关资源
      最近更新 更多