【问题标题】:How can I change the order of the RadioGroup widget appears in HelloFormStuff from Android Dev?如何更改 RadioGroup 小部件出现在 Android Dev 的 HelloFormStuff 中的顺序?
【发布时间】:2011-05-03 15:24:09
【问题描述】:

我正在阅读教程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();
        }
    };

}

【问题讨论】:

    标签: java android widget radio-group


    【解决方案1】:

    你可以试试:

    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"/>
    

    【讨论】:

      【解决方案2】:

      我知道这已经很老了,但我刚开始学习 Android 时遇到了同样的问题。也许这会帮助新人我发现你看不到他们的原因是因为RadioGrouplayout_height的值应该是wrap_content。该示例说使其成为fill_parent。但是您现在可能已经知道,对象的高度将到达屏幕底部。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-16
        • 2021-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多