我在我的android应用程序中有几个单选按钮选项,但我希望它们看起来完全不同。更多像下面的东西(快速模型),你只需点击你想要的单词,它就会使它们变成粗体和下划线。如何创建自定义Android单选按钮?
有谁知道我可以做到这样呢?欢迎所有提示!
我在我的android应用程序中有几个单选按钮选项,但我希望它们看起来完全不同。更多像下面的东西(快速模型),你只需点击你想要的单词,它就会使它们变成粗体和下划线。如何创建自定义Android单选按钮?
有谁知道我可以做到这样呢?欢迎所有提示!
一般来说,要覆盖的默认控件的外观,你需要创建一个文件夹绘制,并把所有的XML定义该文件夹中。然后在布局的RadioButton块内引用该xml文件。
下面是关于如何做一个好的博客文章:
我知道这可能是晚了,但它不是一个理由将解决自己。
1)您需要实现.XML
布局为RadioGroup
和它的RadioButton
秒。将RadioGroup
儿童的方向设置为Horizontal
的值以并排显示按钮。与@null
值设置RadioButton
按钮隐藏默认选择
如下:
<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)为了执行所需的操作,当用户选择任何按钮,我们需要重写为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){
}
}
});
的最终输出将是非常相似的问题图像除了分隔符。
我希望它能帮助。
如果你有答案,请分享。 这将是一个很大的帮助 – Syn3sthete