2016-07-25 3 views
2

Android Studio에서 Android 앱의 기본 라디오 버튼을 설정하고 싶습니다. XML 파일에서 버튼을 설정 했으므로 앱을 만드는 것이 매우 새로운 기능입니다. 앱 열 때 기본 버튼을 설정하는 방법을 알고 싶습니다.Android Studio에서 기본 라디오 버튼 설정

인터넷에서 보았지만 무엇을 해야할지 이해할 수 없습니다. 나는 다음과 같은 것을 원한다. http://cloud.addictivetips.com/wp-content/uploads/2012/05/SoShare-Android-Share.jpg

어떤 코드를 게시하면, 무슨 일이 일어나고 있는지 알려 주시면 감사하니?

+1

버튼의보기 ID를 가져 와서 radiobutton.setChecked (true)와 같이 사실인지 확인하십시오. –

+0

어떻게? 나는 이것에 초급이다 –

+0

라디오 단추와 관련있는 당신의 부호를 풀칠하십시오. –

답변

3

넣어주세요.

Xml을 사용하십시오.

      <RadioGroup 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:id="@+id/radiogroup" 
          android:orientation="horizontal"> 
          <RadioButton 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:text="RadioButton1" 
           android:checked="true" 
           android:id="@+id/radio1" 
           /> 
          <RadioButton 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:text="RadioButton2" 
           android:id="@+id/radio2" 
           /> 
         </RadioGroup> 

사용하여 자바 : 나는 그것이 도움이 될 것입니다 생각 activity

RadioGrop rg= (RadioGroup)v.findViewById(R.id.radio_group); 

    // get selected radio button from radioGroup 
    int selectedId = radioPassanger.getCheckedRadioButtonId(); 

    // find the radiobutton by returned id 
    RadioButton mRadioButton = (RadioButton) v.findViewById(selectedId); 

this-처럼

RadioGroup radiogroup; 

     radiogroup=(RadioGroup)findViewById(R.id.radiogroup) 

     radiogroup.check(R.id.radio1); 
+0

. 이 도움이 –

+0

당신은 오신 것을 환영합니다. – Hari

1

말을이 XML 당신은 단순히 코드 아래에 쓸 수

<RadioGroup 
     android:id="@+id/radiogroup" 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <RadioButton 
     android:id="@+id/radiobutton1" 
     android:checked="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

     <RadioButton 
     android:id="@+id/radiobutton2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" />  
</RadioGroup> 

아래처럼 라디오 버튼으로 구성되어있는 경우 : also.Note이 줄

radiobutton1 =(RadioButton)findViewById(R.id.radiobutton1); 
radiobutton1.setChecked(true); 

그렇지 않으면 당신이 XML 수준에서이 작업을 수행 할 수 있습니다 android:checkedButton="@+id/radiobutton1"

<RadioGroup 
      android:id="@+id/radiogroup" 
      android:orientation="horizontal" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checkedButton="@+id/radiobutton1" 
               > 

      <RadioButton 
      android:id="@+id/radiobutton1" 
      android:checked="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

      <RadioButton 
      android:id="@+id/radiobutton2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" />  
    </RadioGroup> 
+0

nope. 오류가 발생했습니다. 그것은 라디오 버튼을 해결할 수 없다고 말합니다. onCreate에 넣을까요? 나는 여전히 오류가 있습니다. –

0

사용 default-RadioButton으로 설정할 수 있습니다, XML으로

, 자바으로

android:checked="true" 

,

radiobutton1.setChecked(true); 

이 5 월 당신을 도와줍니다.

+0

자바 버전을 사용하면 오류가 발생합니다. xml을 사용하면 java가 검사되었음을 알 수 없습니다. –

+0

오류를 게시 할 수 있습니까? 덕분에 –

0

이 this-

<RadioGroup> 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/radio_group" 
    android:orientation="horizontal"> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="1" 
     android:textSize="12dp" 
     android:id="@+id/r1" 
     android:checked="true"/> 

    <RadioButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="2" 
     android:id="@+id/r2" 
     android:checked="false" 
     android:textSize="12dp" /> 
</RadioGroup> 

처럼 레이아웃에 RadioButton을 설정하고있는 .