[!]たぶん初歩的なミスです。
[Androidアプリ/Java]
###やりたいこと。
- 選択されたラジオボタンに合わせて表示する文字を変える。←これができない。
- ラジオボタンは2つあり、idはそれぞれradio1,radio2。
- radiotextviewというidのTextViewのテキスト内容を変えたい。
###State:
- 初期値の表示はできている。
- エラーは出ていない。
- AIDEで作成。
- こちらのページを参考に作成。
- 正直、Javaはもちろん、プログラミングなんてまともにやったことない。
###Code:
- MAIN.XML
xml
1<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="fill_parent" 3 android:layout_height="fill_parent" 4 android:orientation="vertical"> 5 6 <TextView 7 android:id="@+id/text" 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="@string/init_msg" 11 /> 12 <EditText 13 android:id="@+id/edit" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 /> 17 <Button 18 android:id="@+id/button" 19 android:layout_width="300px" 20 android:layout_height="wrap_content" 21 android:text="@string/btn_label" 22 /> 23 24 <CheckBox 25 android:text="@string/check_label" 26 android:id="@+id/check1" 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 /> 30 31 <RadioGroup 32 android:id="@+id/group" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content"> 35 36 <TextView 37 android:id="@+id/radiotextview" 38 android:layout_width="wrap_content" 39 android:layout_height="wrap_content" 40 android:text="@string/radiotext" 41 /> 42 43 <RadioButton 44 android:text="@string/radio1_label" 45 android:id="@+id/radio1" 46 android:layout_width="wrap_content" 47 android:layout_height="wrap_content" 48 /> 49 <RadioButton 50 android:text="@string/radio2_label" 51 android:id="@+id/radio2" 52 android:layout_width="wrap_content" 53 android:layout_height="wrap_content" 54 /> 55 56 </RadioGroup> 57 58</LinearLayout>
- MAINACTIVITY.JAVA
java
1package com.mycompany.myapp; 2 3import android.app.Activity; 4import android.os.Bundle; 5import android.text.Editable; 6import android.view.View; 7import android.view.View.*; 8import android.widget.*; 9 10 11public class MainActivity extends Activity 12{ 13 private Button button; 14 private EditText edit; 15 private TextView text; 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) 19 { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.main); 22 23 // コンポーネントの設定 24 button = (Button)this.findViewById(R.id.button); 25 edit = (EditText)this.findViewById(R.id.edit); 26 text = (TextView)this.findViewById(R.id.text); 27 28 // イベント組み込み 29 button.setOnClickListener(new MyClickAdapter()); 30 31 // CheckBox 32 CheckBox check = (CheckBox)this.findViewById(R.id.check1); 33 check.setChecked(true); 34 35 // RadioButton 36 TextView radiotextview = (TextView)this.findViewById(R.id.radiotextview); 37 RadioGroup group = (RadioGroup)this.findViewById(R.id.group); 38 RadioButton radio1 = (RadioButton)this.findViewById(R.id.radio1); 39 RadioButton radio2 = (RadioButton)this.findViewById(R.id.radio2); 40 41 radio2.setChecked(true); 42 43 int selid = group.getCheckedRadioButtonId(); 44 RadioButton selradio = (RadioButton)this.findViewById(selid); 45 radiotextview.setText(selradio.getText()); 46 } 47 48 // イベントクラス 49 class MyClickAdapter implements OnClickListener { 50 @Override 51 public void onClick(View v) { 52 Editable s = edit.getText(); 53 text.setText("こんにちは、" + s + "さん。"); 54 } 55 } 56}
- STRINGS.XML
xml
1<?xml version="1.0" encoding="utf-8"?> 2<resources> 3 4 <string name="app_name">el_20161026</string> 5 <string name="hello_world">Hello world!</string> 6 7 <string name="init_msg">お名前をどうぞ。</string> 8 <string name="btn_label">クリック!</string> 9 10 <string name="check_label">チェックボックス</string> 11 12 <string name="radiotext">どのラジオボタンがチェックされたか</string> 13 <string name="radio1_label">ラジオボタンその1</string> 14 <string name="radio2_label">ラジオボタンその2</string> 15 16</resources>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/27 06:47