閱讀899 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Android OnTouchListener觸屏事件接口

    OnTouchListener接口是用來處理手機屏幕事件的監聽接口,當為View的範圍內觸摸按下、抬起或滑動等動作時都會觸發該事件。該接口中的監聽方法簽名如下。

Java代碼:
public boolean onTouch(View v, MotionEvent event)


       參數v:參數v同樣為事件源對象。
       參數event:參數event為事件封裝類的對象,其中封裝了觸發事件的詳細信息,同樣包括事件的類型、觸發時間等信息。

       節中介紹了一個在屏幕中拖動矩形移動的案例,本節將繼續采用該案例的思路,通過監聽接口的方式實現在屏幕上拖動按鈕移動的案例。開發步驟如下。

       創建一個名為Sample的Android項目。

       準備字符串資源,打開strings.xml文件,用下列代碼替換原有代碼。

Java代碼:
<?xml version="1.0" encoding="utf-8"?>
<!-- XML的版本及編碼方式 -->
<resources>
<string name="hello">Hello World, Sample</string>
<!--定義hello字符串 -->
<string name="app_name">Sample</string>
<!--定義app_name字符串-->
<string name="location">位置</string>
<!--定義location字符串-->
</resources>


       說明:與前麵介紹的案例相同,對程序中用到的字符串資源進行定義。
       開發布局文件。打開res/layout目錄下的main.xml,用下列代碼替換其原有代碼。

Java代碼:
<?xml version="1.0" encoding="utf-8"?>
<!-- XML的版本及編碼方式 -->
<AbsoluteLayout
android:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:androhttps://schemas.android.com/apk/res/android" target="_blank">https://schemas.android.com/apk/res/android">
<!-- XML的版本及編碼方式 -->
<Button
android:layout_y="123dip"
android:layout_x="106dip"
android:text="@string/location"
android:layout_height="wrap_content"
android:
android:layout_width="wrap_content"/>
<!-- XML的版本及編碼方式 -->
</AbsoluteLayout>


       說明:該布局文件非常簡單,隻是在一個絕對布局中添加一個按鈕控件即可,需要注意的是應該為該按鈕指定ID,以便在Java代碼中可以得到該按鈕的引用。

       接下來開始開發主要的邏輯代碼。編寫Sample.java文件,其代碼如下所示。

Java代碼:
package wyf.ytl;
//聲明所在包
import android.app.Activity;
//引入相關類
//該處省略了部分類的引入代碼,讀者可以自行查閱隨書光盤中的源代碼\
import android.widget.Button;
//引入相關類
public class Sample extends Activity {
final static int WRAP_CONTENT=-2;
//表示WRAP_CONTENT的常量
final static int X_MODIFY=4;
//在非全屏模式下X坐標的修正值
final static int Y_MODIFY=52;
//在非全屏模式下Y坐標的修正值
int xSpan;
//在觸控筆點擊按鈕的情況下相對於按鈕自己坐標係的
int ySpan;
//X,Y位置
public void onCreate(Bundle savedInstanceState) {
//重寫的onCreate方法
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//設置當前的用戶界麵
Button bok=(Button)this.findViewById(R.id.Button01);
//得到按鈕的引用
bok.setOnTouchListener(
//添加監聽
new OnTouchListener(){
//創建監聽類
public boolean onTouch(View view, MotionEventevent) {
//重寫的監聽方法
switch(event.getAction()){
//監聽事件
case MotionEvent.ACTION_DOWN:
//觸控筆按下
xSpan=(int)event.getX();
//得到X坐標
ySpan=(int)event.getY();
//得到Y坐標
break;
case MotionEvent.ACTION_MOVE:
//觸控筆移動
Button bok=(Button)findViewById(R.id.Button01);
//讓按鈕隨著觸控筆的移動一起移動
ViewGroup.LayoutParams lp= new AbsoluteLayout.LayoutParams( WRAP_CONTENT,
WRAP_CONTENT,(int)event.getRawX()-xSpan-X_MODIFY,(int)event.getRawY()-ySpan-Y_MODIFY ) ;
bok.setLayoutParams(lp);
//設置按鈕的坐標
break;
}
return true;
}
}
);
}

public boolean onKeyDown (int keyCode, KeyEvent event){
//鍵盤鍵按下的方法
Button bok=(Button)this.findViewById(R.id.Button01);
//得到按鈕的引用
bok.setText(keyCode+" Down");
//設置按鈕的文字
return true;
}
public boolean onKeyUp (int keyCode,KeyEvent event){
//鍵盤鍵抬起的方法
Button bok=(Button)this.findViewById(R.id.Button01);
//得到按鈕的引用
bok.setText(keyCode+" Up");
//設置按鈕的文字
return true;
}
public boolean onTouchEvent (MotionEventevent){
//讓按鈕隨觸控筆的移動一起移動
Button bok=(Button)this.findViewById(R.id.Button01);
//得到按鈕引用
ViewGroup.LayoutParams lp=
new AbsoluteLayout.LayoutParams(
//創建
LayoutParams WRAP_CONTENT, WRAP_CONTENT, (int)event.getRawX()-xSpan-X_MODIFY,
//X坐標
(int)event.getRawY()-ySpan-Y_MODIFY
//Y坐標
) ;
bok.setLayoutParams(lp);
return true;
}

}


       第6~10行聲明了程序中需要的一些變量。
       第11~40行重寫了Activity中的onCreate方法,在方法中設置當前的用戶界麵,然後得到按鈕的引用並為其注冊監聽。第16~38行創建監聽器類並重寫onTouch方法,然後根據事件的類型執行不同的操作。
       第41~45行重寫了onKeyDown回調方法,在該方法中得到按鈕的引用並設置按鈕上的文字,第46~50行重寫了onKeyUp回調方法,同樣也是設置按鈕上的文字。
       第51~61行重寫了onTouchEvent回調方法,用來處理屏幕事件的監聽方法,在方法中得到按鈕的引用,然後設置按鈕的坐標。

最後更新:2017-04-02 06:51:49

  上一篇:go Android Notification 基礎
  下一篇:go Linux下去掉^M的方法