首页 > 代码库 > Android 按键式事件

Android 按键式事件

 

 

1、

package com.fish.helloworld;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.util.Log;import android.view.KeyEvent;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;public class MainActivity extends Activity {    private ImageView m_ImageView;    private TextView m_TextView;    private float m_AlphaValue;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_test_key);                m_ImageView = (ImageView)findViewById(R.id.imageView1);        m_TextView = (TextView)findViewById(R.id.textView1);        m_AlphaValue = 0xFF;                m_ImageView.setAlpha(m_AlphaValue);        m_TextView.setText("Alpha = " + m_AlphaValue * 100 / 0xff + "%");    }        @Override    public boolean onKeyDown(int keyCode, KeyEvent msg){                Log.v("KeyCode","KeyCode = " + keyCode);        Log.v("msg","msg = " + msg);                switch(keyCode){        case KeyEvent.KEYCODE_VOLUME_UP:            m_AlphaValue += 20;            break;        case KeyEvent.KEYCODE_VOLUME_DOWN:            m_AlphaValue -= 20;            break;            default:                break;                }                if(m_AlphaValue >= 0xFF) m_AlphaValue = http://www.mamicode.com/0xFF;        if(m_AlphaValue <= 0x00) m_AlphaValue = http://www.mamicode.com/0x0;                m_ImageView.setAlpha(m_AlphaValue);        m_TextView.setText("Alpha = " + m_AlphaValue * 100 / 0xff + "%");                return super.onKeyDown(keyCode, msg);    }}

 

2、

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/textView1"        android:layout_width="110dp"        android:layout_height="wrap_content"        android:layout_weight="0.03"        android:text="TextView" />    <ImageView        android:id="@+id/imageView1"        android:layout_width="241dp"        android:layout_height="195dp"        android:layout_marginRight="37dp"        android:layout_marginTop="176dp"        android:layout_weight="0.06"        android:src="@drawable/ic_launcher" /></LinearLayout>

 3、触摸屏

package com.fish.helloworld;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.util.Log;import android.view.KeyEvent;import android.view.Menu;import android.view.MenuItem;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;public class MainActivity extends Activity {    private TextView m_TextView;    private TextView m_TextView2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.skeleton_activity);                m_TextView = (TextView)findViewById(R.id.textView1);        m_TextView2 = (TextView)findViewById(R.id.textView2);            }        @Override    public boolean onTouchEvent(MotionEvent event){                int action = event.getAction();        float x = event.getX();        float y = event.getY();                m_TextView.setText("Action = " + action);        m_TextView2.setText(x + " + " + y);                return true;    }    }

 

 

 

4、