안드로이드 EditText에 특정 타입 입력받기
main.xml 속성에서 android:inputType= 으로 설정 가능.
A text field for user input. We'll make it display the text entered so far when the "Enter" key is pressed.
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number">
"|" 연산자를 통해 두가지 속성을 같이 적용할 수 있음. (number|numberDecimal)
EditText
EditText
element: <EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>onCreate()
method:
final EditText edittext = (EditText) findViewById(R.id.edittext);
edittext.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
Toast.makeText(HelloFormStuff.this,
edittext.getText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}});
This captures our EditText element from the layout, then adds an on-key listener to it.
The View.OnKeyListener
must define the onKey()
method, which defines the action to be made
when a key is pressed.
In this case, we want to listen for the Enter key (when pressed down), then pop up a Toast
message
with the text from the EditText field.
Be sure to return true after the event is handled, so that the event doesn't bubble-up and get handled
by the View (which would result in a carriage return in the text field).
'0.일반개발' 카테고리의 다른 글
[Android] EditText 이벤트 처리. 2 (0) | 2010.09.14 |
---|---|
[Android] EditText 이벤트 처리. (0) | 2010.09.14 |
안드로이드 웹서버 통신 HTTP (0) | 2010.09.14 |
안드로이드 앱, 리스트에 헤더 만들기 ( 구분 ) (2) | 2010.09.14 |
안드로이드 타원형 박스 (0) | 2010.09.14 |