如何按钮专注于在Android中单击

时间:2011-05-24 12:44:34

标签: android android-layout

在我的布局xml文件中,我创建了一个带有三个背景按钮的屏幕, 现在当用户点击它时。它没有显示任何单击的符号 我怎样才能做到这一点。

提前致谢

4 个答案:

答案 0 :(得分:3)

不确定但是,我们需要通过xml设置悬停

<item
        android:color="hex_color"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_window_focused=["true" | "false"] />

答案 1 :(得分:1)

这是答案。

要指示不同的按钮状态(聚焦,选择等),您可以为每个状态定义不同的图像

将XML文件保存在项目res / drawable /文件夹中,然后将其引用为ImageButton源代码的drawable(在android:src属性中)。 Android将根据按钮的状态和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/button_pressed" /> <!-- pressed -->
   <item android:state_focused="true"
       android:drawable="@drawable/button_focused" /> <!-- focused -->
   <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>

答案 2 :(得分:0)

大家好我自己找到了答案。 我做了什么

    final Button home = (Button) findViewById(R.id.btnmaphome);
    home.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            home.setBackgroundResource(R.drawable.lodclick);
            // rest of the code
        }
    });

这里我有另一张图片,当按钮被点击时,我将其设置为背景按钮上的另一个图像设置为背景,似乎按钮得到了聚焦。 谢谢所有人。

答案 3 :(得分:0)

将这三个xml文件放在drawable文件夹中。 custom_background_focus.xml

{

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

    <stroke
        android:width="1dp"
        android:color="#111111" />

    <gradient
        android:angle="225"
        android:endColor="#fcfcfc"
        android:startColor="#fcfcfc" />

    <corners
        android:bottomLeftRadius="4dp"
        android:bottomRightRadius="4dp"
        android:topLeftRadius="4dp"
        android:topRightRadius="4dp" />

</shape>

custom_background_normal.xml

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

        <stroke
            android:width="1dp"
            android:color="#333333" />

        <gradient
            android:angle="225"
            android:endColor="#999999"
            android:startColor="#999999" />

        <corners
            android:bottomLeftRadius="4dp"
            android:bottomRightRadius="4dp"
            android:topLeftRadius="4dp"
            android:topRightRadius="4dp" />

    </shape>

}

<?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/custom_background_focus" /> <!-- pressed -->
   <item android:state_focused="true"
       android:drawable="@drawable/custom_background_focus" /> <!-- focused -->
   <item android:drawable="@drawable/custom_background_normal" /> <!-- default -->
</selector>
相关问题