BloggerAds

2013年6月16日 星期日

[Android] Android 按鈕按下、彈起狀態及簡單應用

難度:★★☆☆☆☆☆☆☆☆15%
要讓按鈕按下彈起其實非常簡單,用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));

效果圖



相關文章: 

[每日一學] Android Button切換頁面

5 則留言:

  1. 超讚的,謝謝你的教學!

    回覆刪除
  2. 想請教您,若是我要按下button時(不是按著不放,而是按一下放開)改變button上面的文字,再按一下就回復原來文字,是要用甚麼監聽方式?例如原來button上的字是"normal",按一下後變為"stereo",再按一次就回復為normal.

    回覆刪除
  3. 你可以用 togglebutton元件 OnclickListener 監聽
    if (togglebutton.ischecked){
    bt.setText("normal");
    }else{
    bt.setText("stereo");
    }

    回覆刪除
  4. OnTouchListener 和 OnClickListener 能不能一起執行

    按的時候變色然後執行跳頁

    回覆刪除