Sunday, December 22, 2013

Android: How to use Edittext in android


- 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: // Input text to textView from editText
   String al1 = editText.getText().toString();
   textView.setText(al1);
   break;
  
  case R.id.Button2: // Clear All
   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>


- Display
   

No comments:

Post a Comment