Android 基本控件的使用四(仿團購底部菜單App)(RadioGroup)

項目名稱:仿團購底部菜單Appp

項目功能:通過點擊按鈕,使 RadioGroup 上面的TextView 的字題顏色 和 背景顏色發生變化 

項目技術:使用 RadioGroup 的互斥的功能來實現的;RadioButton 裏頭字體顏色的變化和圖標顏色的變化使用過 selector 來實現的

注意:給RadioGroup設置的監聽爲__     OnCheckedChangeListener

開發步驟:

1.寫 selector.xml  colors.xml

2.佈局

3.聲明控件

4.爲控件設置監聽

界面如下:

代碼如下:

deal_selector.xml

<?xmlversion="1.0"encoding="utf-8"?>

<selectorxmlns:android="http://schemas.android.com/apk/res/android">

    

<itemandroid:state_checked="true"

    android:drawable="@drawable/ic_tab_deal_selected"

    ></item>

<item 

    android:drawable="@drawable/ic_tab_deal_unselected"

    ></item>

    

</selector>


nearyby_selector.xml

<?xmlversion="1.0"encoding="utf-8"?>

<selectorxmlns:android="http://schemas.android.com/apk/res/android">

    

<itemandroid:state_checked="true"

    android:drawable="@drawable/ic_tab_nearby_selected"

    ></item>

<item 

    android:drawable="@drawable/ic_tab_nearby_unselected"

    ></item>

    

</selector>


my_selector.xml

<?xmlversion="1.0"encoding="utf-8"?>

<selectorxmlns:android="http://schemas.android.com/apk/res/android">

    

<itemandroid:state_checked="true"

    android:drawable="@drawable/ic_tab_my_selected"

    ></item>

<item 

    android:drawable="@drawable/ic_tab_my_unselected"

    ></item>

    

</selector>



more_selector.xml

<?xmlversion="1.0"encoding="utf-8"?>

<selectorxmlns:android="http://schemas.android.com/apk/res/android">

    

<itemandroid:state_checked="true"

    android:drawable="@drawable/ic_tab_more_selected"

    ></item>

<item 

    android:drawable="@drawable/ic_tab_more_unselected"

    ></item>

    

</selector>



text_color_selector.xml

<?xmlversion="1.0"encoding="utf-8"?>

<selectorxmlns:android="http://schemas.android.com/apk/res/android">


    <itemandroid:state_checked="true"android:color="@color/main_color"></item>

    <itemandroid:color="@color/default_tab_menu_text_color"></item>


</selector>



colors.xml

<?xmlversion="1.0"encoding="utf-8"?>

<resources>

    <color name="main_color">#FF6625</color>

    <colorname="default_tab_menu_text_color">#999999</color>

</resources>



MainActivity.java

package cn.sophia.android;


import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
// 當點擊radioButton 時候,圖標顏色和字體也要發生相應的變化,TextVIew 的字體顏色和 背景顏色也發生相應的變化
public class MainActivity extends Activity {
// 聲明控件
private RadioGroup rgTabmenu;
private TextView tvContent;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化控件
rgTabmenu = (RadioGroup) findViewById(R.id.rg_tab_menu);
tvContent = (TextView) findViewById(R.id.tv_content);
// 爲控件設置監聽
rgTabmenu.setOnCheckedChangeListener(new OnCheckedChangeListener() {


@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
switch (checkedId) {
case R.id.rb_tab_menu_deal:
tvContent.setText("團購");
tvContent.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));
break;


case R.id.rb_tab_menu_nearby:
tvContent.setText("附近");
tvContent.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light));
break;
case R.id.rb_tab_menu_my:
tvContent.setText("我的");
tvContent.setBackgroundColor(getResources().getColor(android.R.color.holo_orange_light));
break;
case R.id.rb_tab_menu_more:
tvContent.setText("更多");
tvContent.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
break;


}
}
});
}


}


發佈了25 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章