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事件響應綜合提示控件Toast應用示例
相關(guān)文章
Android使用ViewStub實現(xiàn)布局優(yōu)化方法示例
這篇文章主要為大家介紹了Android使用ViewStub實現(xiàn)布局優(yōu)化方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
Android使用ViewPager快速切換Fragment時卡頓的優(yōu)化方案
今天小編就為大家分享一篇關(guān)于Android使用ViewPager快速切換Fragment時卡頓的優(yōu)化方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
Android開發(fā)之獲取LayoutInflater對象的方法總結(jié)
這篇文章主要介紹了Android開發(fā)之獲取LayoutInflater對象的方法,結(jié)合實例形式總結(jié)分析了Android獲取LayoutInflater對象的常用技巧,需要的朋友可以參考下2016-02-02
Android游戲開發(fā)實踐之人物移動地圖的平滑滾動處理
玩過rpg游戲的朋友應該都知道RPG的游戲地圖一般都比較大 今天我和大家分享一下在RPG游戲中如何來處理超出手機屏幕大小的游戲地圖。2014-06-06
Android滑動優(yōu)化高仿QQ6.0側(cè)滑菜單(滑動優(yōu)化)
之前的實現(xiàn)只是簡單的可以顯示和隱藏左側(cè)的菜單,但是特別生硬,而且沒有任何平滑的趨勢,那么今天就來優(yōu)化一下吧,加上平滑效果,而且可以根據(jù)手勢滑動的方向來判斷是否是顯示和隱藏2016-02-02

