由于使用小米系统MIUI运行是RadioButton样式跟google Android API自定义的不一样,则我们可以定义任何想要的东东。没有做不到,只有想不到

  • Android 自定义RadioButton
  • Android 自定义RadioButton 实现文字上下左右方向的图片大小设置

单选项框RadioGroup

单选按钮是一种双状态的按钮,可以选择或不选中。在单选按钮没有被选中时,用户能够按下或点击来选中它。但是,与复选框相反,用户一旦选中就不能够取消选中(译者注:可以通过代码来控制,界面上点击的效果是一旦选中之后就不能取消选中了)。

多个单选按钮通常与RadioGroup同时使用。当一个单选组(RadioGroup)包含几个单选按钮时,选中其中一个的同时将取消其它选中的单选按钮

  • RadioGroup 的组事件.RadioGroup 可将各自不同的RadioButton ,设限于同一个Radio 按钮组,同一个RadioGroup 组里的按钮,只能做出单一选择(单选题).

通过Demo来了解---RPcalc

activity_main.xml

<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名" />
<!--建立一个RadioGroup -->
<RadioGroup
android:id="@+id/rg_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!--第一个RadioButton -->
<RadioButton
android:id="@+id/rb_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_weight="2"
android:text="男" />
<!--第二个RadioButton -->
<RadioButton
android:id="@+id/rb_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_weight="2"
android:text="女" />
<!--第三个RadioButton -->
<RadioButton
android:id="@+id/rb_other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_weight="2"
android:text="人妖" />
</RadioGroup>
  • 大致的UI布局,就是用到这些控件

MainActivity.java

public class MainActivity extends ActionBarActivity {

private EditText et_name;
private RadioGroup rg_group; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); <!--找到我们关心的控件 -->
et_name = (EditText) findViewById(R.id.et_name);
rg_group = (RadioGroup) findViewById(R.id.rg_group); Button btn = (Button) findViewById(R.id.btn);
<!--设置按钮点击事件--点击按钮,获取数据,跳转到ResultActivity页面 -->
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
<!--获取用户名 -->
String name = et_name.getText().toString().trim(); <!--判断用户名为空 -->
if (TextUtils.isEmpty(name)) {
Toast.makeText(getApplicationContext(), "用户名不能为空", Toast.LENGTH_LONG).show();
return;
} <!--判断选中的性别-->
int checkedRadioButtonId = rg_group.getCheckedRadioButtonId(); <!--判断具体选中的性别 -->
int sex =0; //定义一个变量默认值为0 作用就是做一个标识
switch (checkedRadioButtonId) {
case R.id.rb_male: //选中的是男
sex =1;
break;
case R.id.rb_female: //选中的是女
sex =2;
break;
case R.id.rb_other: //选中的是人妖
sex =3;
break;
} <!--判断性别 -->
if (sex ==0) {
Toast.makeText(getApplicationContext(), "请选性别", Toast.LENGTH_SHORT).show();
return;
} <!--跳转到ResultActivity--使用显示意图-->
Intent intent = new Intent(MainActivity.this, ResultActiviy.class); <!--要把name和sex传递到结果页面 putExtra(里面可以传递各种类型) -->
intent.putExtra("name", name);
intent.putExtra("sex", sex); <!--开启Activity -->
startActivity(intent);
}
});
} }

奇怪的事情就发生了---在小米系统下的就变化了

  • 这可能是跟小米系统基于Android的MIUI系统有关,可是如果面临这种情况怎么办呢

两种方法思路

一、Android 自定义RadioButton 实现文字上下左右方向的图片大小设置

Button与Textview 我们只是把自定义的图片资源放在Textview的左边,并把Button设置为null来实现

2.设置drawable图像显示在文字的上下左右的位置,如果不想设置,则传递null参数。drawable图片的边界是其自身固定的边界范围。

3.left, top, right, bottom是对于文字上下左右方向的图片大小设置

  1. 常见方法是:在代码中动态设置图片大小。然后设置在布局上。
<span style="font-size:18px;">
mRadioButton.setCompoundDrawables(left, top, right, bottom);
</span>

参数类型都是Drawable,分别是左,上,右,下四个方向要显示的Drawable图片我们查看setCompoundDrawables(left, top, right, bottom)方法:用次方法之前,需要用Drawable.setBounds()方法来为Drawable图片设置边界,即要显示的大小。

  1. 另一种常见方法是:
<span style="font-size:18px;">
setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);
</span>

进入源码查看该方法的具体实现:

<span style="font-size:18px;">public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top,
Drawable right, Drawable bottom) { if (left != null) {
left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight());
}
if (right != null) {
right.setBounds(0, 0, right.getIntrinsicWidth(), right.getIntrinsicHeight());
}
if (top != null) {
top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight());
}
if (bottom != null) {
bottom.setBounds(0, 0, bottom.getIntrinsicWidth(), bottom.getIntrinsicHeight());
}
setCompoundDrawables(left, top, right, bottom);
}</span>
  • 在Demo中应用是这样的--MainActivity.java
et_name = (EditText) findViewById(R.id.et_name);
rg_group = (RadioGroup) findViewById(R.id.rg_group); Drawable ds = getResources().getDrawable(R.layout.item);
setCompoundDrawablesWithIntrinsicBounds(rb_female,ds); //自定义setCompoundDrawablesWithIntrinsicBounds 替换RadioButton图片
public void setCompoundDrawablesWithIntrinsicBounds(RadioButton rb, Drawable left) { if (left != null) {
left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight());
}
rb.setCompoundDrawables(left, null, null, null); //位置是相对应文字来决定的
}

原来这个方法,同样调用了setCompoundDrawables(left, top, right, bottom)方法,并在调用之前,给传入的图片设置了边界范围,即图片自身的大小。再看这个方法的注释:设置drawable图像显示在文字的上下左右的位置,如果不想设置,则传递null参数。drawable图片的边界是其自身固定的边界范围。

二、Android 自定义RadioButton

这个是把自定义的替换掉系统自带的,加载自己的来实现

  1. 第一步,自定义RadioButton图片Drawable---item.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/dx_checkbox_off" />
<item android:state_checked="true" android:drawable="@drawable/dx_checkbox_on" />
</selector>

2.第二步,MainActivity.xml中添加下面代码

RadioButton rb_female = (RadioButton) findViewById(R.id.rb_female);
RadioButton rb_male = (RadioButton) findViewById(R.id.rb_male);
RadioButton rb_other = (RadioButton) findViewById(R.id.rb_other); //这个是把自定义的替换掉系统自带的,加载自己的
rb_male.setButtonDrawable(R.layout.item);
rb_female.setButtonDrawable(R.layout.item);
rb_other.setButtonDrawable(R.layout.item);

欢迎交流,进入博客网站:www.wangsong520.com进行留言交流,并且里面有更多知识分享!

Android 自定义RadioButton实现的更多相关文章

  1. Android 自定义RadioButton的样式

    Android 自定义RadioButton的样式 我们知道Android控件里的button,listview可以用xml的样式自定义成自己希望的漂亮样式. 最近用到RadioButton,利用xm ...

  2. android自定义radiobutton样式文字颜色随选中状态而改变

    主要是写一个 color selector 在res/建一个文件夹取名color res/color/color_radiobutton.xml <selector xmlns:android= ...

  3. android 自定义 radiobutton 文字颜色随选中状态而改变

    主要是写一个 color selector 在res/建一个文件夹取名color res/color/color_radiobutton.xml <?xml version="1.0& ...

  4. Android自定义radiobutton(文字靠左,选框靠右)

    <RadioButton android:id="@+id/rb_never" android:layout_width="fill_parent" an ...

  5. 转:android 自定义RadioButton样式

    http://gundumw100.iteye.com/blog/1146527  上面这种3选1的效果如何做呢?用代码写? 其实有更简单的办法,忘了RadioButton有什么特性了吗? 我就用Ra ...

  6. Android 自定义 radiobutton

    <RadioButton android:id="@+id/radiobutton_pay_method" android:layout_width="30dp&q ...

  7. Android 自定义RadioButton样式

     上面这种3选1的效果如何做呢?用代码写? 其实有更简单的办法,忘了RadioButton有什么特性了吗? 我就用RadioButton实现了如上效果,其实很简单的. 首先定义一张background ...

  8. React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton)

    React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton) 一,需求与简单介绍 在开发项目时发现RN没有给提供RadioButton和Rad ...

  9. android自定义RadioGroup实现可以添加多种布局

    android自带的RadioGroup是继承自LinearLayout,如果布局的时候不是直接写radiobutton,即radiobutton外面还包了一层容器,这时分组是不成功的,因为查找不到r ...

随机推荐

  1. 标准I/O库之临时文件

    ISO C标准I/O库提供了两个函数以帮助创建临时文件. #include <stdio.h> char *tmpnam( char *ptr ); 返回值:指向唯一路径名的指针 FILE ...

  2. 从源码角度深入分析ant

    Ant的基本概念 首先是ant的基本概念:Project,Target,Tasks,Properties,Paths 1.Project <project> build.xml文件最顶层的 ...

  3. 解决Android拍照保存在系统相册不显示的问题

    可能大家都知道我们保存相册到Android手机的时候,然后去打开系统图库找不到我们想要的那张图片,那是因为我们插入的图片还没有更新的缘故,先讲解下插入系统图库的方法吧,很简单,一句代码就能实现 Med ...

  4. Android(java)学习笔记159:Dalivk虚拟机的初始化过程

    1.初始化下面系统函数(调用dvmStartup函数初始化所有相关的函数) 开始学习虚拟机的初始化过程,先从dvmStartup函数开始,这个函数实现所有开始虚拟机的准备工作: dvmAllocTra ...

  5. Python中如何写控制台进度条的整理

    本文实例讲述了Python显示进度条的方法,是Python程序设计中非常实用的技巧.分享给大家供大家参考.具体方法如下: 首先,进度条和一般的print区别在哪里呢? 答案就是print会输出一个\n ...

  6. IIS限制ASP.Net 文件上传大小解决方案,修改IIS7/7.5配置

    当在web.config中设置了 httpruntime 后还是无法成功上传大文件,则要修改IIS的系统config IIS 7 默认文件上传大小是30M 要突破这个限制: 修改IIS的applica ...

  7. Magento入门开发教程

    Modules->模块 Controller->控制器 Model->模型 Magento是这个星球上最强大的购物车网店平台.当然,你应该已经对此毫无疑问了.不过,你可能还不知道,M ...

  8. js 一个关于图片onload加载的事

    前几天一个项目让我头疼了很久,一个关于图片加载时的loading效果,因为不是太懂js,所以在网上各种找资料,但还是不理想,无赖苦心研究,终于有了一点眉目了,虽然个中还有一些道理不懂,至少目的达到了; ...

  9. NChome如何创建单据跟主子表还有扩展开发要怎么弄?

    单据表跟主子表笔记做在笔记本里面 扩展开发在网络备份里面

  10. ASP.NET MVC5总结(一)@HTML和对应的HTML

    HtmlHelper用来在视图中呈现 HTML 控件,主要分为以下几类: 1.ActionLink - 链接到操作方法 @Html.ActionLink("这是一个连接", &qu ...