識別輸入裝置ID與InputDevice裝置(Improved event management)

識別輸入裝置ID與InputDevice裝置(Improved event management)

新建一個繼承Activity類的InputDeviceActivity,並設置佈局文件爲:inputdevice.xml。

在佈局文件中添加一個TextView用於顯示InputDevice返回的數據

    <TextView

        android:id="@+id/inputdevice_tv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:scrollHorizontally="true"

        android:text="@string/show_information"

        android:textSize="22sp" />

之後在Activity代碼中覆寫OnTouchEvent()方法

package lyx.feng.second;

......

public class InputDeviceActivity extends Activity {

    private String temp = "";

    private String enter = "\n";

    private TextView tv = null;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       super.setContentView(R.layout.inputdevice);

       this.tv = (TextView) super.findViewById(R.id.inputdevice_tv);

    }

 

    @Override

    public boolean onTouchEvent(MotionEvent event) {

       InputDevice inputDevice = event.getDevice();

       temp = "";

       temp = temp + "座標:";

       temp = "(" + event.getX() + "," + event.getY() + ")" + enter;

       temp = temp + "設備名稱:" + inputDevice.getName() + enter;

       temp = temp + "設備ID" + inputDevice.getId() + enter;

       temp = temp + "鍵盤類型:" + inputDevice.getKeyboardType() + enter;

      

       temp = temp + "設備描述:" + inputDevice.getDescriptor() + enter;

       this.tv.setText(temp);

       return super.onTouchEvent(event);

    }

}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章