●要讓按鈕按下或彈起其實非常簡單,用OnTouchListener監聽,
並在裡面判斷按下及彈起事件
先貼上簡單的程式碼
package com.learn;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
private Button touch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
touch = (Button) findViewById(R.id.button1);
touch.setOnTouchListener(touchlistener);
}
Button.OnTouchListener touchlistener = new Button.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
touch.setText("按下");
} else if (event.getAction() == MotionEvent.ACTION_UP) {
touch.setText("彈起");
}
return false;
}
};
}
沒按下顯示我是一顆普通按鈕 ,按下去不放開顯示按下,放開則顯示彈起
結果圖
●另外我們可以用這方法去實現一些效果,字體顏色改變、放大縮小、圖片變換等等...
以下是改變圖片大小,以縮放的方式去呈現
代碼
Button.OnTouchListener changeListener = new Button.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float scaleWidth = 0;
float scaleHeight = 0;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
img.buildDrawingCache();
Bitmap bmp = img.getDrawingCache();
int width = bmp.getWidth();
int height = bmp.getHeight();
// 放大為1.2倍
scaleWidth = (float) 1.2;
scaleHeight = (float) 1.2;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newbm = Bitmap.createBitmap(bmp, 0, 0, width, height,
matrix, true);
img.setImageBitmap(newbm);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
img.buildDrawingCache();
Bitmap bmp = img.getDrawingCache();
int width = bmp.getWidth();
int height = bmp.getHeight();
scaleWidth = (float) 0.85;
scaleHeight = (float) 0.85;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newbm = Bitmap.createBitmap(bmp, 0, 0, width, height,
matrix, true);
img.setImageBitmap(newbm);
}
return false;
}
};
結果圖

●改變字體大小及顏色,顏色有兩種方式可以實作
1.把顏色寫程式碼內
Button.OnTouchListener textlistener = new Button.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Color c = null ;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
text.setText("放大,藍色");
tv.setTextSize(30);
tv.setTextColor(c.BLUE);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
text.setText("縮小,黑色");
tv.setTextSize(20);
tv.setTextColor(c.BLACK);
}
return false;
}
};
2.把顏色在styles 資源檔裡
Button.OnTouchListener textlistener = new Button.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
text.setText("放大,藍色");
tv.setTextSize(30);
tv.setTextColor(getResources().getColor(R.color.blue));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
text.setText("縮小,黑色");
tv.setTextSize(20);
tv.setTextColor(getResources().getColor(R.color.black));
效果圖
相關文章:
超讚的,謝謝你的教學!
回覆刪除謝謝^^
刪除想請教您,若是我要按下button時(不是按著不放,而是按一下放開)改變button上面的文字,再按一下就回復原來文字,是要用甚麼監聽方式?例如原來button上的字是"normal",按一下後變為"stereo",再按一次就回復為normal.
回覆刪除你可以用 togglebutton元件 OnclickListener 監聽
回覆刪除if (togglebutton.ischecked){
bt.setText("normal");
}else{
bt.setText("stereo");
}
OnTouchListener 和 OnClickListener 能不能一起執行
回覆刪除按的時候變色然後執行跳頁