2013-10-01 4 views
3

내 안드로이드 앱에 몇 가지 라디오 버튼 옵션이 있지만 완전히 다른 모습으로 보이기를 바랍니다. 당신이 원하는 단어를 간단히 클릭하면 대담하고 밑줄이 그어진 다음과 같은 (빠른 모형). 나는 이런 식으로 뭔가를 달성 할 수있는 방법 맞춤형 Android 라디오 버튼을 만드는 방법은 무엇인가요?

enter image description here

는 사람은 알고 있나요? 모든 팁을 환영합니다!

+0

이 답변을 얻으려면 공유하십시오. 큰 도움이 될 것입니다. – Syn3sthete

답변

1

일반적으로 기본 위젯의 모양을 덮어 쓰려면 드로어 블 폴더를 만들고 해당 폴더에 모든 XML 정의를 저장해야합니다. 그런 다음 레이아웃의 RadioButton 블록에서 해당 XML 파일을 참조하십시오.

여기에 모든 작업을 수행하는 방법에 대한 좋은 블로그 게시물입니다 :

http://blog.devminded.com/posts/custom-android-radiobutton

0

나는 늦을 수도 있습니다 알고 있지만, 자신을 위해 솔루션을 유지하는 이유가 없습니다.

1) 당신은 RadioGroup에 대한 .XML 레이아웃을 구현해야하며 RadioButton들입니다. 어린이 방향을 Horizontal 값으로 설정하여 버튼을 나란히 표시하십시오. 당신의 Activity 클래스에서

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

     <RadioButton 
      android:id="@+id/i_like_radiobutton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:button="@null" 
      android:padding="10dp" 
      android:text="I Like" 
      android:textColor="@android:color/holo_orange_light" /> 

     <RadioButton 
      android:id="@+id/i_dont_like_radiobutton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:button="@null" 
      android:padding="10dp" 
      android:text="I Dont Like" 
      android:textColor="@android:color/holo_orange_light" /> 
    </RadioGroup> 

2)이 그들을 초기화하고 자신의 리스너를 설정합니다 @null 값으로 RadioButton 버튼을 설정하면 (자), 디폴트의 선택 다음으로 enter image description here

을 숨 깁니다. 청취자는 RadioButton 변경 사항의 변경 사항을 추적하고 상태에 따라 UI 변경 사항을 선택 또는 선택 취소해야합니다. 다음과 같이

RadioGroup myRadioGroup = (RadioGroup) findViewById(R.id.my_radiogroup); 
    RadioButton likeRadioButton = (RadioButton) findViewById(R.id.i_like_radiobutton); 
    RadioButton dontLikeRadioButton = (RadioButton) findViewById(R.id.i_dont_like_radiobutton); 

    //Like button listener 
    likeRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (isChecked) { 
       //Make the text underlined 
       SpannableString content = new SpannableString(getString(R.string.like_text)); 
       content.setSpan(new UnderlineSpan(), 0, content.length(), 0); 
       buttonView.setText(content); 
       //Make the text BOLD 
       buttonView.setTypeface(null, Typeface.BOLD); 
      } else { 
       //Change the color here and make the Text bold 
       SpannableString content = new SpannableString(getString(R.string.like_text)); 
       content.setSpan(null, 0, content.length(), 0); 
       buttonView.setText(content); 
       buttonView.setTypeface(null, Typeface.NORMAL); 

      } 
     } 
    }); 

    //Don't Like button listener 
    dontLikeRadioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (isChecked) { 
       //Change the color here and make the Text bold 
       SpannableString content = new SpannableString(getString(R.string.like_text)); 
       content.setSpan(new UnderlineSpan(), 0, content.length(), 0); 
       buttonView.setText(content); 
       buttonView.setTypeface(null, Typeface.BOLD); 
      } else { 
       //Change the color here and make the Text bold 
       SpannableString content = new SpannableString(getString(R.string.like_text)); 
       content.setSpan(null, 0, content.length(), 0); 
      buttonView.setText(content); 
      buttonView.setTypeface(null, Typeface.NORMAL); 

      } 
     } 
    }); 

3)는 이제 RadioButton이 변경됩니다 것은 색상 그리고 그것은 자동 상태입니다에 텍스트 스타일을 따라합니다. 원하는 경우 사용자 정의를 추가 할 수 있습니다. 필요한 작업을 수행하는 사용자가 버튼 중 하나를 선택하면 들어

4), 우리는 다음과 같이 RadioGroup에 대한 setOnCheckedChangeListener 메서드를 재정의해야합니다

genderRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      if (checkedId == R.id.i_dont_like_radiobutton) { 
       //Do some actions 
      } else if (checkedId == R.id.i_like_radiobutton){ 

      } 
     } 
    }); 

최종 출력은 매우 유사합니다 구분 기호를 제외한 질문 이미지.

enter image description here

는 나는 그것이 도움이되기를 바랍니다.

관련 문제