Nik*_*tas 34 android pressed button
我有一个按钮,我希望当我按下它时,它会保持按下状态(Froyo上的绿色).
有帮助吗?
mycodes_Button = (Button) findViewById(R.id.mycodes);
...
if (saved_Button.isPressed())
{
saved_Button.setFocusable(true);
}
Run Code Online (Sandbox Code Playgroud)
像这样的东西?
bee*_*tra 45
我有一个带有自定义背景的按钮,并最终使用所选状态.该状态适用于所有视图.
要使用此功能,您必须将自定义按钮背景定义为状态列表:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false" android:state_focused="false"
android:state_pressed="false"><bitmap ... /></item>
<item android:state_selected="true"><bitmap ... /></item>
<item android:state_focused="true"><bitmap ... /></item>
<item android:state_pressed="true"><bitmap ... /></item>
</selector>
Run Code Online (Sandbox Code Playgroud)
然后使用该背景,假设它/res/drawable/button_bg.xml在您的布局文件中,您使用:
...
<Button android:background="@drawable/button_bg" ... />
...
Run Code Online (Sandbox Code Playgroud)
在您的代码中,您可以切换到onClick侦听器中的(取消)选定状态:
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setSelected(true);
// normal click action here
}
});
Run Code Online (Sandbox Code Playgroud)
在激活状态的本意相匹配较好,但仅适用于Android 3.x和更高.
Ven*_*ddy 38
使用以下代码.这很有用.
mycodes_Button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mycodes_Button.setPressed(true);
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57664 次 |
| 最近记录: |