2

嗨,我在 xml 中有一个按钮,我OnTouchListener在我的活动中使用来获取button按下和释放。但问题是当我按下按钮时背景颜色没有改变。当我扩展可能的活动时OnClickListener,背景正在发生变化。谁能告诉我的代码有什么问题。

 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>

很抱歉这篇冗长的帖子....

4

5 回答 5

5
@Override
public boolean onTouch(View v, MotionEvent event) {

    switch(event.getAction()) {

        case MotionEvent.ACTION_DOWN:
            v.setPressed(true);
            //doYourWorkHere();
            break;
        case MotionEvent.ACTION_UP:
            v.setPressed(false);
            //doYourWorkHere();
            break;
        }

    return true;/* true to deactivate other listeners at the same time; false otherwise */
}
于 2016-08-11T15:44:37.007 回答
2

使用 Ontouch,您不会获得按钮的 Clicked 状态

使用 Onclick Coz ,在 onclick 方法中,按钮的按下状态将为 true

实际上我需要点击按钮并释放

使用它来获得点击和释放

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(new OnClickListener() {

GPIO_0_B.setOnTouchListener(new OnTouchListener() {

我该如何处理它的 id,其实我有不止一个按钮

这样做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() 方法之外:- 新手的信息)

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);
            }
        }
    }
}
于 2013-09-17T11:24:14.677 回答
2

只需将布局设置为 xml 中 Button 的背景即可。在您的可绘制文件夹中有一个 xml,以 eg:clickedstate.xml 命名,如下所示

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

<item     android:state_enabled="false"     
android:drawable="@drawable/default_bgnd" />

<item     android:state_focused="true"        
android:drawable="@drawable/new_green" />

<item     android:state_pressed="true"        
android:drawable="@drawable/new_green" />

<item     android:state_checked="true"        
android:drawable="@drawable/new_green"/>

<item     android:state_selected="true"        
android:drawable="@drawable/new_green" /> 
</selector> 

在您的 xml 中,只需为您的按钮执行此操作

android:background="@drawable/clickedstate"
于 2013-09-17T11:37:46.400 回答
1

您可以通过 onclick 事件侦听器使用以下代码来实现此目的:

       StateListDrawable states = new StateListDrawable(); 

        Drawable image_normal = //drawable of the image
        Drawable image_focused =  //drawable of the image
        Drawable image_pressed =  //drawable of the image

        states.addState(new int[] {android.R.attr.state_pressed},image_focused);
        states.addState(new int[] {android.R.attr.state_focused},image_pressed);       
        states.addState(new int[] { },image_normal);  
        buttonObj.setBackgroundDrawable(states);
于 2013-09-17T11:33:52.367 回答
0

试试这个代码。

 @Override
 public boolean onTouch(View view, MotionEvent event) {
     view.setSelected(true);
     ...
    return true;

 }
于 2014-11-23T09:56:22.520 回答