본문 바로가기

코드조각모음

[android004] 키입력 예제

import android.app.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.view.View.OnKeyListener;
import android.widget.*;

public class KeyTest extends Activity implements OnKeyListener
{
    StringBuilder builder = new StringBuilder();
    TextView textView;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        textView = new TextView(this);
        textView.setText("Press key (if you have some)!");
        textView.setOnKeyListener(this);
        textView.setFocusableInTouchMode(true);
        //textView.requestFocus();
        setContentView(textView);
    }
   
   
    @Override
    public boolean onKey(View view, int keyCode, KeyEvent event)
    {
        builder.setLength(0);
        switch(event.getAction())
        {
        case KeyEvent.ACTION_DOWN:
            builder.append("down, ");
            break;
        case KeyEvent.ACTION_UP:
            builder.append("up, ");
            break;
        }
        builder.append(event.getKeyCode());
        builder.append(", ");
        builder.append((char)event.getUnicodeChar());
        String text = builder.toString();
        Log.d("KeyTest", text);
        textView.setText(text);
       
        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK)
            return false;
        else
            return true;
    }
}