0

我正在阅读教程HelloFormStuff。我开始在添加 RadioGroup 小部件时遇到问题。在移动了一些括号后,我终于让它工作了。

当我尝试添加最后 (2) 个小部件时,我发现如果我尝试将它们添加到 RadioGroup 下方的 main.xml 中,它们将不会出现在应用程序中。我想我可以称之为完成并继续前进,但我花时间输入所有代码(不是 ctrl C,ctrl P),该死的,小部件应该出现在我告诉他们的地方!为什么我不能在 RadioGroup 下方添加小部件?

public class HelloFormStuff extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final EditText edittext = (EditText) findViewById(R.id.edittext);
    edittext.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                (keyCode == KeyEvent.KEYCODE_ENTER)) {
              // Perform action on key press
              Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show();
              return true;
            }
            return false;
        }
    });
    final CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
    checkbox.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks, depending on whether it's now checked
            if (((CheckBox) v).isChecked()) {
                Toast.makeText(HelloFormStuff.this, "Selected", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(HelloFormStuff.this, "Not selected", Toast.LENGTH_SHORT).show();
            }
        }
    });
    final RadioButton radio_red = (RadioButton) findViewById(R.id.radio_red);
    final RadioButton radio_blue = (RadioButton) findViewById(R.id.radio_blue);
    radio_red.setOnClickListener(radio_listener);
    radio_blue.setOnClickListener(radio_listener);

    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            Toast.makeText(HelloFormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show();
        }
    });
    final ToggleButton togglebutton = (ToggleButton) findViewById(R.id.togglebutton);
    togglebutton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            if (togglebutton.isChecked()) {
                Toast.makeText(HelloFormStuff.this, "Checked", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(HelloFormStuff.this, "Not checked", Toast.LENGTH_SHORT).show();
            }
        }
    });
    final RatingBar ratingbar = (RatingBar) findViewById(R.id.ratingbar);
    ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            Toast.makeText(HelloFormStuff.this, "New Rating: " + rating, Toast.LENGTH_SHORT).show();
        }
    });
    }   
    private OnClickListener radio_listener = new OnClickListener() {
        public void onClick(View v) {
            // Perform action on clicks
            RadioButton rb = (RadioButton) v;
            Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
        }
    };

}

4

2 回答 2

2

你可以试试:

Toast.makeText(HelloFormStuff.this,
  ((RadioButton) v).getText(),
  Toast.LENGTH_SHORT).show();

如果它崩溃了,但我认为您的代码没有任何问题。这是 main.xml,一切都应该显示出来:

<CheckBox android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="check it out" />
<RadioGroup
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical">
  <RadioButton android:id="@+id/radio_red"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Red" />
  <RadioButton android:id="@+id/radio_blue"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Blue" />

</RadioGroup>
<ToggleButton android:id="@+id/togglebutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="Vibrate on"
    android:textOff="Vibrate off"/>
    <RatingBar android:id="@+id/ratingbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:stepSize="1.0"/>

于 2011-03-13T19:52:36.570 回答
1

我知道这真的很老了,但我刚开始学习 Android,我遇到了同样的事情。也许这会帮助新人我发现你看不到他们的原因是因为layout_heightfor the的值RadioGroup应该是wrap_content。这个例子说做它fill_parent。但正如您现在可能知道的那样,对象的高度将到达屏幕底部。

@Duy 的 main.xml 是正确的,但我只是想指出确切的原因。

于 2011-10-28T01:41:46.143 回答