android的RadioGroup講解
這個主要是如何替換fragment的demo。效果圖如下(下麵的tabhost和上麵的bar不屬於這次的內容,這個是我做的一個應用程序框架的一部分,有需要的或者想研究研究的可以私下聯係),主要是講解中間的內容怎麼實現,即點擊上麵的RadioGroup,下麵的內容一起改變(改變的是XML中的布局,這樣的話下麵三個的布局完全可以自己定義)
1.首先在主界麵的xml中添加一個RadioGroup,裏麵添加三個RadioButton即可
- <RadioGroup
- android:id="@+id/radioGroup1"
- style="@style/layout_full"
- android:layout_margin="5dp"
- android:background="@drawable/rounded_edittext"
- android:orientation="horizontal"
- android:padding="5dp" >
- <RadioButton
- android:id="@+id/radio0"
- style="@style/layout_horizontal"
- android:layout_gravity="center_horizontal"
- android:layout_weight="1"
- android:checked="true"
- android:text="均分" />
- <RadioButton
- android:id="@+id/radio1"
- style="@style/layout_horizontal"
- android:layout_gravity="center_horizontal"
- android:layout_weight="1"
- android:text="個人" />
- <RadioButton
- android:id="@+id/radio2"
- style="@style/layout_horizontal"
- android:layout_gravity="center"
- android:layout_weight="1"
- android:text="借貸" />
- </RadioGroup>
其中
- android:background="@drawable/rounded_edittext"
這一句是給這個RadioGroup添加一個帶圓角的邊框
rounded_edittext.xml的代碼如下
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="https://schemas.android.com/apk/res/android"
- android:shape="rectangle" >
- <solid android:color="#ffffff" />
- <corners android:radius="7dip" />
- <stroke
- android:width="2px"
- android:color="#000000" />
- </shape>
放置在drawable文件夾下即可
2.下麵的內容由三個xml定義好的布局來呈現,這三個xml的布局可以自己來寫 ,我就很簡單地建了三個,做例子用
speeddial_fragment_pay1.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_margin="5dp"
- android:background="@drawable/rounded_edittext"
- android:orientation="vertical" >
- <Button
- android:id="@+id/Button04"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:layout_centerVertical="true"
- android:text="Button" />
- </RelativeLayout>
3.(重要)在主布局文件中添加Fragment的載體,比如一個framlayout,負責承載fragment
在上麵的RadioGroup的布局下增加:
- <FrameLayout
- android:id="@+id/fragment_container"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
這樣布局就完成了
4.由於Fragment的特性,我們要新建三個自己的Fragment,都繼承自Fragment
SpeeddialFragmentOne.java
- package com.gracker.fragment;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import com.gracker.tabactivity.R;
- public class SpeeddialFragmentOne extends Fragment {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- return inflater.inflate(R.layout.speeddial_fragment_pay1, container, false);
- }
- }
這個Fragment非常簡單,沒有添加任何的邏輯,僅僅隻是在onCreateView的要布局然後以View返回。Fragment有很多方法,可以根據自己的需要進行重載,這裏就不多說了,自己到用的時候自然就知道了。
類似地,建立另外兩個Fragment ,改變的僅僅是
- return inflater.inflate(R.layout.speeddial_fragment_pay1, container, false);
5.在主Activity中調用:
MainActivity.java
- /**
- * 主Activity
- *
- * @author Gracker Gao
- * @date 2012.8.15
- */
- package com.gracker.hostactivity;
- import android.app.Activity;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.RadioGroup;
- import android.widget.RadioGroup.OnCheckedChangeListener;
- import com.gracker.fragment.SpeeddialFragmentOne;
- import com.gracker.fragment.SpeeddialFragmentThree;
- import com.gracker.fragment.SpeeddialFragmentTwo;
- import com.gracker.tabactivity.R;
- public class MainActivity extends Activity {
- private final String TAG = "SpeedDialActivity";
- private RadioGroup mRadioGroup;
- private SpeeddialFragmentTwo mSpeeddialFragmentTwo;
- private SpeeddialFragmentOne mSpeeddialFragmentOne;
- private SpeeddialFragmentThree mSpeeddialFragmentThree;
- private FragmentTransaction transaction;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.speeddial);
- init_date();
- setupWidgets();
- }
- private void init_date(){
- transaction = getFragmentManager()
- .beginTransaction();
- if (null == mSpeeddialFragmentOne) {
- mSpeeddialFragmentOne = new SpeeddialFragmentOne();
- }
- transaction.add(R.id.fragment_container,
- mSpeeddialFragmentOne);
- // Commit the transaction
- transaction.commit();
- }
- private void setupWidgets() {
- mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
- mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- // TODO Auto-generated method stub
- switch (checkedId) {
- case R.id.radio0:
- Log.v(TAG, "setupWidgets():radio0 clicked");
- if (null == mSpeeddialFragmentOne) {
- mSpeeddialFragmentOne = new SpeeddialFragmentOne();
- }
- transaction = getFragmentManager()
- .beginTransaction();
- transaction.replace(R.id.fragment_container,
- mSpeeddialFragmentOne);
- // Commit the transaction
- transaction.commit();
- break;
- case R.id.radio1:
- Log.v(TAG, "setupWidgets():radio1 clicked");
- if (null == mSpeeddialFragmentTwo) {
- mSpeeddialFragmentTwo = new SpeeddialFragmentTwo();
- }
- transaction = getFragmentManager()
- .beginTransaction();
- transaction.replace(R.id.fragment_container,
- mSpeeddialFragmentTwo);
- // Commit the transaction
- transaction.commit();
- break;
- case R.id.radio2:
- Log.v(TAG, "setupWidgets():radio2 clicked");
- if (null == mSpeeddialFragmentThree) {
- mSpeeddialFragmentThree = new SpeeddialFragmentThree();
- }
- transaction = getFragmentManager()
- .beginTransaction();
- transaction.replace(R.id.fragment_container,
- mSpeeddialFragmentThree);
- // Commit the transaction
- transaction.commit();
- break;
- default:
- break;
- }
- }
- });
- }
- @Override
- protected void onResume() {
- // TODO Auto-generated method stub
- super.onResume();
- }
- @Override
- protected void onDestroy() {
- // TODO Auto-generated method stub
- super.onDestroy();
- // dataEncapsulation.closeDataBase_speedDial();
- }
- }
在RadioGroup的onCheckedChangeLinsteer中,切換Fragment。關於Fragment的一些操作,比如增加,刪除,替換等等,可以參照這個帖子:https://www.eoeandroid.com/thread-71642-1-1.html 講的很詳細,我也不想重複。
這個Demo就不提供下載了,畢竟不是很難,所有的東西都交代了,自己敲一遍收獲總是比打開別人的代碼來研究要好的多。
例子中有什麼錯誤的地方歡迎指正。
最後更新:2017-04-03 12:53:35