Android - 图片按钮点击

Android - ImageButton clicking

我正在尝试使我的图像按钮看起来不错。我尝试了几种不同的方法,但它们看起来都不对。这是一个圆形图像,我想让它看起来像是可以按下的。这是我到目前为止所得到的。 android:textAppearance="?android:attr/textAppearanceLarge" 通过显示方形框使其看起来像是按下了按钮。如何完成这项工作?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:layout_centerHorizontal="true">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/connectButton"
            android:background="?android:attr/selectableItemBackground"
            android:src="@drawable/myImage"
            android:layout_gravity="center"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Press To Connect To Device"
            android:id="@+id/connectMessage"
            android:layout_gravity="center_horizontal|top"
            android:layout_marginTop="10dp" />
    </FrameLayout>

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:visibility="gone"
        android:indeterminateDrawable="@drawable/progress" >
    </ProgressBar>

</RelativeLayout>

与其在 XML 中执行此操作,为什么不在 java 代码中使用 OnClickListener 执行此操作?

您可以有两个图像,"regular button" 和一个 "pressed button" 图像。

然后单击您可以将图像更改为 "pressed button" 图像并持续指定的时间(1 - 3 秒),然后在适用的情况下将其更改回 "regular button"

您可以使用 .xml 文件作为按钮的可绘制对象。只是 select 按钮的背景就像 XML 一样:

android:background="@drawable/selector1"

然后将可绘制文件夹中的 selector1.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="@color/cyan"/> <!-- pressed state -->
<item android:state_focused="true" 
      android:drawable="@color/magenta"/> <!-- focused state -->
<item android:drawable="@android:color/transparent"/> <!-- default state -->
</selector>

您还可以选择不同的图片作为按钮背景。您也可以同时使用它:Button & ImageButton.

希望对您有所帮助! :)

这绝不是最佳方法,但我发现一个不错的小方法看起来符合我的要求。我为按下的按钮和默认设置了两个 xml(代码重复!)

round_button.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="200dp" />
    <solid android:color="#FFFFFF" />
    <stroke
        android:width="5dip"
        android:color="#000000" />

    <gradient
        android:startColor="#000000"
        android:centerColor="#FFFFFF"
        android:endColor="#000000"
        android:angle="90">
    </gradient>
</shape>

round_button_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="200dp" />
    <solid android:color="#FFFFFF" />
    <stroke
        android:width="8dip"
        android:color="#000000" />

    <gradient
        android:startColor="#FFFFFF"
        android:centerColor="#000000"
        android:endColor="#FFFFFF"
        android:angle="90">
    </gradient>
</shape>

现在当你创建按钮时添加这个。虽然不是最佳方法,但此方法允许不同类型的用户点击操作和样式。

connectButton.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent event) {
                    if(event.getAction() == MotionEvent.ACTION_DOWN){
                        connectButton.setBackgroundResource(R.drawable.round_button_press);
                        return true;
                    }else if(event.getAction() == MotionEvent.ACTION_UP){
                        connectButton.setBackgroundResource(R.drawable.round_button);
                        connect();
                        return true;
                    }else{
                        Log.e(TAG, event.getAction() + "");
                        return true;
                    }

                }
            });