Android和QUOT; STATE_ pressed"仅在第二次尝试 [英] android "state_pressed" works only in second attempt

查看:180
本文介绍了Android和QUOT; STATE_ pressed"仅在第二次尝试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个菜单键和一个较长的STATE_ pressed状态的运动类(0.5秒)。只要一切工作正常,但出现一个问题:

我已经为preSS第一次在我的按钮,STATE_ pressed不会在这个第一次工作,然后在第二次尝试我的code正常工作和STATE_ pressed与工程0,5秒的时间。

我怎样才能使它它可以作用于第一preSS?我认为tehre与hover.xml文件,并与setBackgroundDrawable?

组合存在问题

感谢所有提前乌拉圭回合的帮助!

这是我hover.XML绘制

 <?XML版本=1.0编码=UTF-8&GT?;

 <项目的android:state_focused =假
      机器人:STATE_ pressed =真
      机器人:可绘制=@绘制/ buttonstyle_ pressed/><项目机器人:可绘制=@绘制/的ButtonStyle/>

这是我的Java code

 按钮menubutton_start;    menubutton_start =(按钮)FindViewById(R.id.menustart);
 menubutton_start.setOnClickListener(新View.OnClickListener(){ 公共无效的onClick(视图v){  menubutton_start.setBackgroundDrawable(getResources()getDrawable(R.drawable.hover));
   最后的处理程序处理程序=新的处理程序();        handler.postDelayed(新的Runnable(){              公共无效的run(){               意图myIntent =新意图(GameActivity.this,NextActivity.class);
               GameActivity.this.startActivity(myIntent);                    }                },500); //处理程序可运行新的结束()
    } //的OnClick结束()
}); // setOnClickListener结束


解决方案

在$ p如果您应用样式按钮$ pssed状态会自动发生。

您不应该手动设置pressed状态在code在所有。

I've one menu button and a motion class for a longer state_pressed state (0,5 sec.). So long all works fine, but one problem occurs :

I've to press first time on my button, state_pressed doesn't work at this first time then on the second attempt my code works correctly and state_pressed works with 0,5 seconds duration.

How can I make it that it works on the first press? I think tehre is a problem with the hover.xml file and in combination with the setBackgroundDrawable?

Thanks all in advance for ur help!

This is my hover.XML drawable

<?xml version="1.0" encoding="utf-8"?>

<item android:state_focused="false" 
      android:state_pressed="true" 
      android:drawable="@drawable/buttonstyle_pressed" />

<item android:drawable="@drawable/buttonstyle" />

And this is my java code

    Button menubutton_start;

    menubutton_start = (Button) FindViewById(R.id.menustart);


 menubutton_start.setOnClickListener(new View.OnClickListener() {

 public void onClick(View v) {     

  menubutton_start.setBackgroundDrawable(getResources().getDrawable(R.drawable.hover));


   final Handler handler = new Handler();            

        handler.postDelayed(new Runnable() {

              public void run() {

               Intent myIntent = new Intent(GameActivity.this, NextActivity.class);
               GameActivity.this.startActivity(myIntent);

                    }

                }, 500);  // end of Handler new Runnable()


    }  // end of OnClick()


});  // end of setOnClickListener

解决方案

The pressed state will happen automatically if you apply that style to the button.

You shouldn't have to manually set the pressed state in your code at all.

这篇关于Android和QUOT; STATE_ pressed&QUOT;仅在第二次尝试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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