Android中的Button自訂點擊效果

來源:互聯網
上載者:User

原來聽很多人說Button不能使用自訂的點擊或選中效果,所以做下測試

結果顯示Button很強大

完全可以使用這種靈活的方法

 方法一

1.放在drawable下的selector.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/temp1" />
 <item android:state_pressed="false" android:state_focused="false"
  android:drawable="@drawable/temp2" />
 <item android:state_focused="true" android:drawable="@drawable/temp3" />
 <item android:state_focused="false" android:drawable="@drawable/temp4" />
</selector>

 

2.布局檔案main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button
 android:drawableTop="@drawable/shouru"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/button"
 android:background="@drawable/selector"/>
</LinearLayout>

 

3.只是為了測試,所以效果不是很好,Button部分狀態如下:

 

①初始化的時候預設顯示的按鈕效果:

 

 

②點擊後釋放顯示的效果

 

 

③點擊不放時的效果

 

 

 

 方法二

1.布局檔案main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Button
 android:id="@+id/button"
 android:drawableTop="@drawable/shouru"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/button"
 android:background="@drawable/temp4"/>
</LinearLayout>

 

2.主要的java代碼,實現點擊效果:
   Button button = (Button) this.findViewById(R.id.button);
   button.setOnTouchListener(new Button.OnTouchListener(){
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    if(event.getAction() == MotionEvent.ACTION_DOWN){   
                    v.setBackgroundResource(R.drawable.temp1);   
                    Log.i("TestAndroid Button", "MotionEvent.ACTION_DOWN");
                }   
                else if(event.getAction() == MotionEvent.ACTION_UP){   
                    v.setBackgroundResource(R.drawable.temp2); 
                    Log.i("TestAndroid Button", "MotionEvent.ACTION_UP");
                } 
    return false;
   }
  });

這個方法實現的效果和上面的一樣,大家可以視具體情況來選擇

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.