2012年7月12日木曜日

ラジオボタン

ボタンをクリックすると、エディットテキスト1とエディットテキスト2をデータ(float型)をとりだす。ラジオボタンで選択された計算(足し算、または引き算)をして、その結果をエディットテキスト3に表示する。



********************  StudyAndroidActivity.java  ********************
package lesson.jp;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
import android.view.View.*;
import android.widget.RadioGroup;
public class StudyAndroidActivity extends Activity {
   
    private EditText edit1, edit2, edit3;
    private Button btn1;
    private RadioGroup rGroup1;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        edit1 = (EditText) findViewById(R.id.editText1);
        edit2 = (EditText) findViewById(R.id.editText2);
        btn1 = (Button) findViewById(R.id.button1);
        edit3 = (EditText) findViewById(R.id.editText3);
        rGroup1 =(RadioGroup) findViewById(R.id.radioGroup1);
       
        btn1.setOnClickListener(new ClickBtn());
    }
    class ClickBtn implements OnClickListener{
        @Override
        public void onClick(View v){
            String s1 = edit1.getText().toString();
            String s2 = edit2.getText().toString();
            float f1 = Float.valueOf(s1);
            float f2 = Float.valueOf(s2);
            float f3;
            int rId = rGroup1.getCheckedRadioButtonId();
           
            switch(rId) {
            case R.id.radio0:
                f3 = f1 + f2;
                break;
            case R.id.radio1:
                f3 = f1 - f2;
                break;
            default:
                f3 = 0;
                break;
            }
           
            edit3.setText(Float.toString(f3));
        }
    }
}

********************  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" >
    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="numberDecimal" >
        <requestFocus />
    </EditText>
    <EditText
        android:id="@+id/editText2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="numberDecimal" />
    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/radio0" />
        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio1" />
    </RadioGroup>
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1" />
    <EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="numberDecimal" />
</LinearLayout>

********************  string.xml  ********************
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">StudyAndroid</string>
    <string name="button1">ボタン</string>
    <string name="radio0">足し算</string>
    <string name="radio1">引き算</string>
</resources>


0 件のコメント:

コメントを投稿