的Andr​​oid如何改变OnTouchListener按钮背景 [英] Android How to change Button Background on OnTouchListener

查看:275
本文介绍了的Andr​​oid如何改变OnTouchListener按钮背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个XML格式的按钮,我使用 OnTouchListener 在我的活动获得按钮 preSS和释放。但问题是,当我preSS按钮在背景颜色没有改变。在那里,当我用延长 OnClickListener 5月的活动背景正在发生变化。任何一个能告诉什么错我的code。

Hi I have a button in xml, and I am using OnTouchListener in my activity to get button press and release. But the problem is that when I press the button the background color is not changing. Where as when I extend may activity with OnClickListener the background is changing. Can any one tell what wrong with my code.

 public class pushbuttonActivity extends Activity implements OnTouchListener {
@Override
        public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
            setContentView(R.layout.push_button_layout);
            GPIO_0_B  = (Button) findViewById(R.id.GPIO_0);
        GPIO_0_B.setOnTouchListener((OnTouchListener) this); 
     }

    public boolean onTouch(View v,MotionEvent event) {
            switch(v.getId()) {
                case R.id.GPIO_0 :  GPIOPORT=0;
                            break;

            default      :  break;                  
            }

            if(event.getAction() == MotionEvent.ACTION_DOWN) {
               //Do something on touch    
                 } else if (event.getAction() == MotionEvent.ACTION_UP) {
                  //Do something in release
                 } 
            return true;
        }

push_button_layout.xml

<RelativeLayout .........
.................
      <Button
        android:id="@+id/GPIO_0"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_button"
        android:textStyle="bold"
        android:textSize="14sp"
        android:text="GPIO 0" 
        android:layout_marginTop="15dp" 
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="50dp"
       />

round_button.xml

<?xml version="1.0" encoding="utf-8" ?> 
     <selector xmlns:android="http://schemas.android.com/apk/res/android">


         <!--  Non focused states 
      --> 
      <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/round_button_unfocused" /> 
      <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/round_button_unfocused" /> 

       <!--  Focused states 
      --> 
      <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/round_button_focus" /> 
      <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/round_button_focus" /> 
     <!--  Pressed 
      --> 
      <item android:state_pressed="true" android:drawable="@drawable/round_button_press" /> 
    </selector>

round_button_focus.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
  <stroke 
        android:width="1dp"
        android:color="#FF404040" /> 
  <corners 
        android:radius="6dp" /> 

  <size android:height="40dp"
       android:width="40dp"/>

  <gradient
        android:startColor="#FF6800" 
        android:centerColor="#FF8000" 
        android:endColor="#FF9700" 
        android:angle="90" /> 
</shape>

round_button_ press.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
  <stroke 
        android:width="2dp"
        android:color="#FF606060" /> 
  <corners 
        android:radius="40dp" /> 
  <gradient
        android:startColor="#FF0000" 
        android:centerColor="#FF0000" 
        android:endColor="#FF0000" 
        android:angle="90" /> 
</shape>

round_button_unfocus.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
  <stroke 
        android:width="2dp"
        android:color="#FF606060" /> 
  <corners 
        android:radius="40dp" /> 
  <gradient
        android:startColor="#550000" 
        android:centerColor="#550000" 
        android:endColor="#550000" 
        android:angle="90" /> 
</shape>

和遗憾的冗长后....

And sorry for the lengthy post....

推荐答案

使用Ontouch你不会得到你的按钮的点击状态下的

Using Ontouch you wont get the Clicked state of your button

使用的onclick堂妹,在OnClick方法pressed按钮的状态将是真正的

use Onclick Coz , In onclick method Pressed state of button will be true

其实我需要点击按钮和释放

用这个来获得点击和释放

use this to get click and release

 GPIO_0_B.setOnClickListener(new OnClickListener() {
@Override
public boolean onTouch(View v, MotionEvent event) 
  {
    if(event.getAction() == MotionEvent.ACTION_DOWN) {

        GPIO_0_B.setBackground(R.drawable.round_button_focus);


    } else if (event.getAction() == MotionEvent.ACTION_UP) {

        GPIO_0_B.setBackground(R.drawable.round_button_unfocused);
    }
}
};

有关触摸听者改变这一行 GPIO_0_B.setOnClickListener(新OnClickListener(){

For touch listner change this line GPIO_0_B.setOnClickListener(new OnClickListener() {


GPIO_0_B.setOnTouchListener(新OnTouchListener(){

`  how can I do with its id, Actually I have more than one button`

在OnCreate中做到这一点()

Do this in oncreate()

   {
     Button GPIO_0_B1 =(Button) findViewById(R.id.youridfromxml);
     Button GPIO_0_B1 =(Button) findViewById(R.id.youridfromxml);

    GPIO_0_B1.setOnClickListener(getOnClickDoSomething(GPIO_0_B1);
   GPIO_0_B2.setOnClickListener(getOnClickDoSomething(GPIO_0_B2);
 }

现在

创造新的方法(外onCreate()方法: - 对于菜鸟资讯)

create new method (outside oncreate() method:- info for noobs)

      View.OnClickListener getOnClickDoSomething(final Button Gpbot)  
{
    return new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {

        Gpbot.setBackground(R.drawable.round_button_focus);


        } else if (event.getAction() == MotionEvent.ACTION_UP) {

             Gpbot.setBackground(R.drawable.round_button_unfocused);
     }
        }
        }
        }

这篇关于的Andr​​oid如何改变OnTouchListener按钮背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆