- EditText로 사용자로 부터 입력받아 글을 화면에 뿌려주는 간단한 예제
- Java 소스
public class Ex03_EditTextActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)this.findViewById(R.id.Button1);
button.setOnClickListener(this);
Button button1 = (Button)this.findViewById(R.id.Button2);
button1.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
EditText editText = (EditText)this.findViewById(R.id.Edit1);
TextView textView = (TextView)this.findViewById(R.id.TextView1);
switch(arg0.getId()){
case R.id.Button1: //editText를 textView에 넣어주는 버튼
String al1 = editText.getText().toString();
textView.setText(al1);
break;
case R.id.Button2: //모든것을 clear
editText.setText("");
textView.setText("");
break;
}
}
}
-xml 소스
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/TextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="글자를 Edit에 치세요" />
<EditText
android:id="@+id/Edit1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="글자를 입력하세요" />
<Button
android:id="@+id/Button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="click" />
<Button
android:id="@+id/Button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="clear" />
</LinearLayout>
- 실행화면
'JAVA > Android' 카테고리의 다른 글
안드로이드 전화번호부 연락처 추가 만들기 예제 (3) | 2012.01.02 |
---|---|
안드로이드 RelativeLayout 예제 따라하기 (0) | 2012.01.02 |
안드로이드 RadioGroup 예제 따라하기 (0) | 2011.12.29 |
안드로이드 LinearLayout 예제 따라하기 (0) | 2011.12.26 |
안드로이드 ImageView 예제 따라하기 (0) | 2011.12.23 |