2012年6月29日金曜日

TextEdit と Button の勉強

ボタンをクリックすると、エディットテキスト1の内容を読み込みエディットテキスト2に表示する



********************  StudyAndroidActivity.java  ********************
package lesson.jp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Button;
import android.text.Editable;
import android.view.View;
import android.view.View.*;
public class StudyAndoridActivity extends Activity {
    /** Called when the activity is first created. */
    EditText edit1;
    Button btn1;
    EditText edit2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        edit1 = (EditText) findViewById(R.id.editText1);
        btn1 = (Button) findViewById(R.id.button1);
        edit2 = (EditText) findViewById(R.id.editText2);
       
        btn1.setOnClickListener(new ClickBtn());
    }
   
    class ClickBtn implements OnClickListener{
        @Override
        public void onClick(View v){
            Editable s = edit1.getText();
            edit2.setText(s+"です");
        }
    }
}
********************  main.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"
        android:text="@string/hello" />
    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >
        <requestFocus />
    </EditText>
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />
</LinearLayout>
********************  string.xml  ********************
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, StudyAndoridActivity!</string>
    <string name="app_name">StudyAndorid</string>
</resources>


0 件のコメント:

コメントを投稿