Android 自定義RadioButton實現


由於使用小米系統MIUI運行是RadioButton樣式跟google Android API自定義的不一樣,則我們可以定義任何想要的東東。沒有做不到,只有想不到

  • Android 自定義RadioButton
  • Android 自定義RadioButton 實現文字上下左右方向的圖片大小設置

單選項框RadioGroup

API

單選按鈕是一種雙狀態的按鈕,可以選擇或不選中。在單選按鈕沒有被選中時,用戶能夠按下或點擊來選中它。但是,與復選框相反,用戶一旦選中就不能夠取消選中(譯者注:可以通過代碼來控制,界面上點擊的效果是一旦選中之后就不能取消選中了)。

多個單選按鈕通常與RadioGroup同時使用。當一個單選組(RadioGroup)包含幾個單選按鈕時,選中其中一個的同時將取消其它選中的單選按鈕

  • RadioGroup 的組事件.RadioGroup 可將各自不同的RadioButton ,設限於同一個Radio 按鈕組,同一個RadioGroup 組里的按鈕,只能做出單一選擇(單選題).

通過Demo來了解---RPcalc

UI布局

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進行留言交流,並且里面有更多知識分享!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM