犹豫现在项目中用到的较多,而在网上搜了一些感觉都不是自己想要的,所以自己就写了一个!犹豫刚接触安卓时间不长,请打牛无视!
package com.example.bottm;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements
OnCheckedChangeListener {
private RadioGroup radiogroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();// 初始化控件
}
/**
*
*/
private void initView() {
radiogroup = (RadioGroup) findViewById(R.id.radiogroup);
radiogroup.setOnCheckedChangeListener(this);
}
// 监听rediogroup的选择改变!
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (radiogroup.getCheckedRadioButtonId()) {
case 2131099651:// 首页
Fargmento fragment = new Fargmento();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fargment, fragment).commit();
break;
case 2131099652:// 实时监控
Fragment1 fragment1=new Fragment1();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fargment, fragment1).commit();
break;
case 2131099653:// 在线解答的界面
break;
case 2131099654:// 专家在线解答的界面
break;
case 2131099655:// 搜索的界面
break;
case 213109956:// 更多的界面
break;
default:
break;
}
}
}
这一出现的一个问题就是,当我们继承activity的时候,
RadioGroup得到当前选中的对象是一个int值,而当我们继承fragmentactivity的时候就得到的是一个内存的地址,所以最终使用<pre name="code" class="java">radiogroup.getCheckedRadioButtonId()这个方法就解决的哪个问题!我底部的菜单采用的是readgroud这个控件来实现的!
<pre name="code" class="html"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ra"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/fargment"
android:layout_width="match_parent"
android:layout_height="418dp"
android:orientation="vertical" >
</LinearLayout>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/header_more_list"
android:gravity="center_vertical"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio_index"
style="@style/maintabt"
android:drawableTop="@drawable/icon_1_n"
android:text="首页" />
<RadioButton
android:id="@+id/radio_monotor"
style="@style/maintabt"
android:drawableTop="@drawable/icon_0_n"
android:text="实时监控" />
<RadioButton
android:id="@+id/radio_solution"
style="@style/maintabt"
android:drawableTop="@drawable/icon_2_n"
android:text="在线解答" />
<RadioButton
android:id="@+id/radio_expertOnline"
style="@style/maintabt"
android:drawableTop="@drawable/icon_3_n"
android:text="专家在线" />
<RadioButton
android:id="@+id/radio_query"
style="@style/maintabt"
android:drawableTop="@drawable/icon_4_n"
android:text="搜索" />
<RadioButton
android:id="@+id/radio_more"
style="@style/maintabt"
android:drawableTop="@drawable/icon_5_n"
android:text="更多" />
</RadioGroup>
</RelativeLayout>