2015-09-19 8 views
1

Я изучаю разработку Android, и я пытаюсь научиться пользоваться переключателями. Когда я запускаю код ниже, я вижу строку «Посещение?» но я не вижу никаких переключателей или их ярлыков. Что мне не хватает?Android Radio Buttons Not Displaying

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> 

ответ

2

Когда я запускаю код ниже, я вижу строку "приду?" но я не вижу никаких радиокнопки или их метки

RadioButton's не видны, потому что все пространство заполнено TextView из-за android:layout_height="match_parent".

Изменить TextView layout height на android:layout_height="wrap_content", чтобы заставить его работать.

+0

Благодарим за помощь! – Angel