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 = 0xFF;
        if(m_AlphaValue <= 0x00) m_AlphaValue = 0x0;
        
        m_ImageView.setAlpha(m_AlphaValue);
        m_TextView.setText("Alpha = " + m_AlphaValue * 100 / 0xff + "%");
        
        return super.onKeyDown(keyCode, msg);
    }
}
昵    称:
验证码:

相关文档: