Android中的Button自定義點擊效果實例代碼
方法一
1.放在drawable下的selector.xml文件
<android="http://schemas.android.com/apk/res/Android">
android:drawable="@drawable/temp2" />
2.布局文件main.xml
<http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<android:drawableTop="@drawable/shouru"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:background="@drawable/selector"/>
方法二
1.布局文件main.xml
[code]
<http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<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"/>
2.主要的java代碼,實現(xiàn)點擊效果:
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;
}
});
- Android編程之Button控件配合Toast控件用法分析
- Android定制RadioButton樣式三種實現(xiàn)方法
- Android點擊Button實現(xiàn)功能的幾種方法總結(jié)
- Android 控件(button)對齊方法實現(xiàn)詳解
- Android自定義格式顯示Button的布局思路
- Android實現(xiàn)點擊Button產(chǎn)生水波紋效果
- android之自定義Toast使用方法
- android自定義toast(widget開發(fā))示例
- android開發(fā)教程之實現(xiàn)toast工具類
- Android編程基礎(chǔ)之簡單Button事件響應(yīng)綜合提示控件Toast應(yīng)用示例
相關(guān)文章
Android學習筆記-保存文件(Saving Files)
這篇文章主要介紹了Android中保存文件(Saving Files)的方法,需要的朋友可以參考下2014-10-10android實現(xiàn)手機App實現(xiàn)拍照功能示例
本篇文章主要介紹了android實現(xiàn)手機App實現(xiàn)拍照功能示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02Android ViewPager實現(xiàn)頁面左右切換效果
這篇文章主要為大家詳細介紹了Android ViewPager實現(xiàn)頁面左右切換效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04Android使用Intent傳大數(shù)據(jù)簡單實現(xiàn)詳解
這篇文章主要為大家介紹了Android使用Intent傳大數(shù)據(jù)簡單實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03Android 關(guān)于“NetworkOnMainThreadException”問題的原因分析及解決辦法
這篇文章主要介紹了Android 關(guān)于“NetworkOnMainThreadException”的相關(guān)知識,本文介紹的非常詳細,具有參考借鑒價值,感興趣的朋友一起學習吧2016-02-02