使用表单创建对象,将其添加到另一个 activity 上的列表
Creating object using a form, adding it to a list on another activity
我想从单选组内单选按钮的文本中检索值。这听起来很简单,但我无法让它工作,因为它总是给我一个空值。
这是我的代码:
public class NewMovie extends FragmentActivity {
RadioGroup rating;
RadioButton selectedRating;
int selectedRatingIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_movie_layout);
rating = (RadioGroup) findViewById(R.id.rating);
onClickListenerButton();
}
public void onClickListenerButton()
{
selectedRatingIndex = rating.getCheckedRadioButtonId();
final RadioButton selectedRating = (RadioButton) findViewById(selectedRatingIndex);
intent.putExtra("rating", selectedRating.getText().toString());
setResult(RESULT_OK, intent);
finish();
}
}
XML:
<RadioButton
android:id="@+id/U"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/U" />
<RadioButton
android:id="@+id/PG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/PG" />
<RadioButton
android:id="@+id/r12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/r12" />
<RadioButton
android:id="@+id/r15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/r15" />
<RadioButton
android:id="@+id/r18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/r18" />
</RadioGroup>
由于某些原因总是空指针异常。
不知道它是否有助于上下文,但我正在尝试从 activity 1 到 activity 2,在 activity 2(我在上面显示的那个)显示无线电组,然后用户提交其中一个答案,然后在 activity 1.
中使用该答案
您必须在单选按钮中设置默认值 select,请按以下方法尝试,
将所有单选按钮放在 xml
的单选组中
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/U"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/U"
android:checked="true" /> // deafult select
//other radio buttons
</RadioGroup>
启动activity中的所有单选按钮
radioU=(RadioButton) findViewById(R.id.U);
检查按钮点击事件中的selection
public void onClickListenerButton(View v) {
if(radioU.isChecked())
{
intent.putExtra("rating", getResources().getString(R.string.U));
}
else if(radioPG.isChecked())
{
intent.putExtra("rating", getResources().getString(R.string.PG));
}
setResult(RESULT_OK, intent);
finish();
}
我想从单选组内单选按钮的文本中检索值。这听起来很简单,但我无法让它工作,因为它总是给我一个空值。
这是我的代码:
public class NewMovie extends FragmentActivity {
RadioGroup rating;
RadioButton selectedRating;
int selectedRatingIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_movie_layout);
rating = (RadioGroup) findViewById(R.id.rating);
onClickListenerButton();
}
public void onClickListenerButton()
{
selectedRatingIndex = rating.getCheckedRadioButtonId();
final RadioButton selectedRating = (RadioButton) findViewById(selectedRatingIndex);
intent.putExtra("rating", selectedRating.getText().toString());
setResult(RESULT_OK, intent);
finish();
}
}
XML:
<RadioButton
android:id="@+id/U"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/U" />
<RadioButton
android:id="@+id/PG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/PG" />
<RadioButton
android:id="@+id/r12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/r12" />
<RadioButton
android:id="@+id/r15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/r15" />
<RadioButton
android:id="@+id/r18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/r18" />
</RadioGroup>
由于某些原因总是空指针异常。
不知道它是否有助于上下文,但我正在尝试从 activity 1 到 activity 2,在 activity 2(我在上面显示的那个)显示无线电组,然后用户提交其中一个答案,然后在 activity 1.
中使用该答案您必须在单选按钮中设置默认值 select,请按以下方法尝试,
将所有单选按钮放在 xml
的单选组中<RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/U" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/U" android:checked="true" /> // deafult select //other radio buttons </RadioGroup>
启动activity中的所有单选按钮
radioU=(RadioButton) findViewById(R.id.U);
检查按钮点击事件中的selection
public void onClickListenerButton(View v) { if(radioU.isChecked()) { intent.putExtra("rating", getResources().getString(R.string.U)); } else if(radioPG.isChecked()) { intent.putExtra("rating", getResources().getString(R.string.PG)); } setResult(RESULT_OK, intent); finish(); }