Android 按钮状态在 Java 中以编程方式(非 XML) [英] Android button states programmatically in Java (not XML)

查看:30
本文介绍了Android 按钮状态在 Java 中以编程方式(非 XML)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为state_pressed"定义Android按钮图像Java中的android:state_focused"?

How does one define Android button image for the "state_pressed" "android:state_focused" in Java?

例如,如何在 Java 中实现 XML from 的等价物

For example, how would one accomplish the equivalent in Java for the XML from

http://developer.android.com/reference/android/widget/ImageButton.html

 <?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/button_pressed" /> <!-- pressed -->
     <item android:state_focused="true"
           android:drawable="@drawable/button_focused" /> <!-- focused -->
     <item android:drawable="@drawable/button_normal" /> <!-- default -->
 </selector>

推荐答案

只需使用StateListDrawable的addState方法

Just use addState method of StateListDrawable

StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[] {android.R.attr.state_pressed}, 
      getResources().getDrawable(R.drawable.phone));

你可以使用下面的常量作为这个方法的第一个参数

You can use constants below for the first parameter of this method

android.R.attr.state_accelerated
android.R.attr.state_activated
android.R.attr.state_active
android.R.attr.state_drag_can_accept
android.R.attr.state_drag_hovered
android.R.attr.state_enabled
android.R.attr.state_first
android.R.attr.state_focused
android.R.attr.state_hovered
android.R.attr.state_last
android.R.attr.state_middle
android.R.attr.state_pressed
android.R.attr.state_selected
android.R.attr.state_single
android.R.attr.state_window_focused

这篇关于Android 按钮状态在 Java 中以编程方式(非 XML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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