【问题标题】:how to make a difference on Imagebutton click如何在 Imagebutton 点击​​上有所作为
【发布时间】:2015-04-23 07:45:11
【问题描述】:

我使用下面的代码将我的按钮设计为圆形,然后我想在按钮上显示一些文本,所以我使用了框架布局,然后我在按钮上放置了一些文本。 现在的挑战是我的按钮仍然存在。它对点击没有任何影响 其中drawable和src都用于圆形按钮和按钮上的图像(分别)。

现在我的按钮不会对点击我的设计产生任何影响。

<FrameLayout>
<ImageButton
android:id="@+id/btn_profile_view"
android:layout_width="70dp"
android:layout_height="67dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:onClick="compareButtonClick"
android:background="@drawable/roundedbutton"
android:src="@drawable/ic_action_person" />
<TextView
android:layout_width="45dp"
android:layout_height="20dp"
android:layout_gravity="bottom"
android:layout_marginLeft="38dp"
android:clickable="false"
android:text="@string/profile"
android:textColor="@color/btn_txt" >
</TextView>
</FrameLayout>


<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<solid android:color="#D3D3D3" />

<corners
    android:bottomLeftRadius="8dip"
    android:bottomRightRadius="8dip"
    android:topLeftRadius="8dip"
    android:topRightRadius="8dip" />

【问题讨论】:

  • 为什么没有指定 framelayout 尺寸属性?
  • 并在您尝试获取 onclick 回调的位置显示您的代码
  • 我不需要框架布局属性,因为我将框架布局放在表格布局、表格行布局中,并且我使用表格行中的权重属性在单行上显示 3 个图像按钮。
  • jave 代码只是 onclicklistner 从一个活动移动到另一个活动......在每个按钮上

标签: android xml imagebutton


【解决方案1】:

在这里你会得到你的点击回调,你不需要在任何地方设置点击监听器

public void compareButtonClick(View v){
    Toast.makeText(this, "Image Button Click", Toast.LENGTH_SHORT).show();
}

【讨论】:

    【解决方案2】:

    这可能会有所帮助,但您必须根据需要对其进行修改。

    1) 在drawable 文件夹中创建roundedbutton.xml

        <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    
    <solid android:color="#D3D3D3" />
    
    <corners
        android:bottomLeftRadius="8dip"
        android:bottomRightRadius="8dip"
        android:topLeftRadius="8dip"
        android:topRightRadius="8dip" />
    </shape>
    

    2) 在drawable 文件夹中创建button_normal.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item android:drawable="@drawable/roundedbutton" android:state_focused="true" android:state_pressed="true"/>
        <item android:drawable="@drawable/ic_launcher" android:state_focused="false" android:state_pressed="true"/>
        <item android:drawable="@drawable/ic_launcher" android:state_focused="true"/>
        <item android:drawable="@drawable/roundedbutton" android:state_focused="false" android:state_pressed="false"/>
    
        <!-- 2 and 3 pressed and selected -->
    
    
    </selector>
    

    3) 将button_normal.xml 添加为按钮的背景

    android:background="@drawable/button_normal"
    

    【讨论】:

      【解决方案3】:

      用不同的drawable创建一个选择器来处理不同的状态;启用、禁用、单击、聚焦等

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-14
        • 2016-05-04
        • 1970-01-01
        • 1970-01-01
        • 2013-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多