2013-10-01 82 views
3

我在我的android应用程序中有几个单选按钮选项,但我希望它们看起来完全不同。更多像下面的东西(快速模型),你只需点击你想要的单词,它就会使它们变成粗体和下划线。如何创建自定义Android单选按钮?

enter image description here

有谁知道我可以做到这样呢?欢迎所有提示!

+0

如果你有答案,请分享。 这将是一个很大的帮助 – Syn3sthete

回答

0

我知道这可能是晚了,但它不是一个理由将解决自己。

1)您需要实现.XML布局为RadioGroup和它的RadioButton秒。将RadioGroup儿童的方向设置为Horizontal的值以并排显示按钮。与@null值设置RadioButton按钮隐藏默认选择 enter image description here

如下:

<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)在你Activity类,它们初始化并设置自己的听众。监听者应该跟踪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)为了执行所需的操作,当用户选择任何按钮,我们需要重写为RadioGroupsetOnCheckedChangeListener方法如下:

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

我希望它能帮助。