问题描述
我刚刚创建了一个自定义按钮,并更改了不同的状态.但是,当我按下按钮(在实际设备上尝试过)时,形状更改被延迟了,例如300毫秒.有人知道它可能来自哪里吗?
button.xml:
<?xml version ="1.0" encoding ="utf-8"?><选择器xmlns:android ="http://schemas.android.com/apk/res/android">< item android:state_pressed ="true" android:drawable ="@ drawable/pressed_button_primary"/>< item android:state_focused ="true" android:drawable ="@ drawable/pressed_button_primary"/>< item android:drawable ="@ drawable/normal_button_primary"/></selector>
normal_button_primary.xml:
<?xml version ="1.0" encoding ="utf-8"?>< shape xmlns:android ="http://schemas.android.com/apk/res/android" android:shape ="rectangle">< solid android:color =#FFCF01"/></形状>
pressed_button_primary.xml:
<?xml version ="1.0" encoding ="utf-8"?>< shape xmlns:android ="http://schemas.android.com/apk/res/android" android:shape ="rectangle">< solid android:color =#FEBF48"/></形状>
积极性:
公共类MainActivity扩展了ActionBarActivity {按钮login_button;按钮signup_button;私有静态最终字符串CONSOLETAG ="consoleDebug:";@Override受保护的void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//点击注册按钮signup_button =(按钮)findViewById(R.id.signupButton);signup_button.setOnClickListener(new View.OnClickListener(){public void onClick(View v){//Nothin}});//点击登录按钮login_button =(Button)findViewById(R.id.loginButton);login_button.setOnClickListener(new View.OnClickListener(){public void onClick(View v){//Nothin}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu){//膨胀菜单;这会将项目添加到操作栏(如果有).getMenuInflater().inflate(R.menu.menu_main,menu);返回true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item){//单击操作栏项.动作栏将//自动处理主页"/向上"按钮上的点击,//当您在AndroidManifest.xml中指定父活动时.int id = item.getItemId();返回super.onOptionsItemSelected(item);}public void goToSignup(View view){Intent intent = new Intent(this,SignupActivity.class);startActivity(intent);}}
在此处找到解决方案: https://stackoverflow.com/a/22027628/658323
发生此问题是因为系统试图检测您是要按下视图还是要执行滚动事件,因此它将等待一段时间,然后将该事件注册为"press"
./p>
解决方案是从 ACTION_DOWN
上的代码中动态注册新闻事件,同时保持选择器xml不变:
yourSelectableView.setOnTouchListener(new View.OnTouchListener(){@Overridepublic boolean onTouch(查看v,MotionEvent事件){开关(event.getActionMasked()){案例MotionEvent.ACTION_DOWN:{v.setPressed(true);休息;}}返回false;//我们返回false,以便点击监听器将处理该事件}});
I just created a custom button and changed the different states. But when I pressed the button (I tried on a real device), the shape change is delayed, something like 300 ms. Does anyone know where it could come from?
button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/pressed_button_primary" />
<item android:state_focused="true" android:drawable="@drawable/pressed_button_primary" />
<item android:drawable="@drawable/normal_button_primary"/>
</selector>
normal_button_primary.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#FFCF01" />
</shape>
pressed_button_primary.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#FEBF48" />
</shape>
Acitivity:
public class MainActivity extends ActionBarActivity {
Button login_button;
Button signup_button;
private static final String CONSOLETAG = "consoleDebug : ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Click on signup button
signup_button = (Button) findViewById(R.id.signupButton);
signup_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Nothin
}
});
// Click on login button
login_button = (Button) findViewById(R.id.loginButton);
login_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Nothin
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
public void goToSignup(View view) {
Intent intent = new Intent(this, SignupActivity.class);
startActivity(intent);
}
}
Found the solution here: https://stackoverflow.com/a/22027628/658323
The issue happens because the system is trying to detect whether you want to press the view or to perform a scroll event, therefore it will wait some time before registering the event as "press"
.
The solution is to register the press event dynamically from your code on ACTION_DOWN
, while keeping the selector xml as it is:
yourSelectableView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN: {
v.setPressed(true);
break;
}
}
return false; //we return false so that the click listener will process the event
}
});
这篇关于按下按钮时Android延迟问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!