繁体   English   中英

Android单选按钮不显示

[英]Android Radio Buttons Not Displaying

我正在学习Android开发,并且正在尝试学习如何使用单选按钮。 当我运行以下代码时,我看到字符串“正在参加?” 但我看不到任何单选按钮或其标签。 我想念什么?

MainActivity.java

package com.example.andriod.assignment1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void onRadioButtonClicked(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();
        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.yes_radio:
                if (checked) {
                    //your code here
                    Toast.makeText(getApplicationContext(), "Thanks for coming!", Toast.LENGTH_LONG).show();
                    break;
                }
            case R.id.no_radio:
                if (checked) {
                    //your code here
                    Toast.makeText(getApplicationContext(), "See you next time!", Toast.LENGTH_LONG).show();
                    break;
                }
        }
    }
}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="Attending?" android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton
            android:id="@+id/yes_radio"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="@string/yes" />
        <RadioButton
            android:id="@+id/no_radio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="@string/no" />
    </RadioGroup>

</LinearLayout>

当我运行以下代码时,我看到字符串“正在参加?” 但我看不到任何单选按钮或它们的标签

因为android:layout_height="match_parent"所以所有的空间都由TextView填充,因此RadioButton's按钮不可见。

将TextView布局height更改为android:layout_height="wrap_content"以使其正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM