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;
}
}
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;
}
}
'코드조각모음' 카테고리의 다른 글
[android006] 어셋(Asset) 예제 (0) | 2011.11.06 |
---|---|
[andorid005] 가속도계 예제 (0) | 2011.11.06 |
[android003] 멀티 터치 예제 (0) | 2011.11.06 |
[android002] 싱글 터치 예제 (0) | 2011.11.06 |
[android001] 액티비티 생명 주기 예제 (0) | 2011.11.06 |