首页 > 应用开发 > IT综合 > 直播商城源码如何实现数据的单项选择
在直播商城源码中,有很多需要用户选择的共鞥,那么单项选择是怎么实现的呢?
MainActivity如下:
package cc.testradiogroup;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.app.Activity;
/**
* Demo描述:
* 利用自定义RadioGroup实现单选
*
* 参考资料:
* 1 http://blog.csdn.net/xiaanming
* 2 http://bbs.51cto.com/thread-954128-1.html
*
* Thank you very much
*/
public class MainActivity extends Activity {
private RadioGroup mRadioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mRadioGroup=(RadioGroup) findViewById(R.id.radioGroup);
mRadioGroup.setOnCheckedChangeListener(new RadioButtonOnCheckedChangeListenerImpl());
}
// 监听单选的变化
private class RadioButtonOnCheckedChangeListenerImpl implements OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton) findViewById(group.getCheckedRadioButtonId());
String currentSelected = rb.getText().toString();
System.out.println("现在选中是:" + currentSelected);
}
}
}
main.xml如下:
<?xml version="1.0" encoding="utf-8"?> <!-- 步骤如下: --> <!--1: android:button="@null" 去掉自带的图标 --> <!--2:android:drawableRight="@drawable/line" 在文字的右边设置图片--> <!--3: android:drawablePadding="10dip" 图片与文字间的距离--> <!--4:android:layout_marginLeft="-17dip" 每个RadioButton距离左边缘或者距其左RadioButton的距离--> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:text="测试自定义的RadioGroup来实现单选功能" > </TextView> <RadioGroup android:id="@+id/radioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@drawable/bg" > <RadioButton android:id="@+id/cai" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="-17dip" android:checked="true" android:button="@null" android:drawableRight="@drawable/line" android:drawablePadding="10dip" android:text="菜" android:textColor="@drawable/text_selector" > </RadioButton> <RadioButton android:id="@+id/tang" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="-15dip" android:button="@null" android:drawableRight="@drawable/line" android:drawablePadding="10dip" android:text="汤" android:textColor="@drawable/text_selector"> </RadioButton> <RadioButton android:id="@+id/zhushi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="-15dip" android:button="@null" android:drawableRight="@drawable/line" android:drawablePadding="10dip" android:text="主 食" android:textColor="@drawable/text_selector" > </RadioButton> <RadioButton android:id="@+id/zhou" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="-15dip" android:button="@null" android:drawableRight="@drawable/line" android:drawablePadding="15dip" android:text="粥" android:textColor="@drawable/text_selector" > </RadioButton> </RadioGroup> </LinearLayout>
text_selector.xml如下:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:color="#ffffff" android:state_checked="true"/> <item android:color="#000000"/> </selector>
这样我们就实现了直播商城源码简单的单选功能。
声明:本文由云豹科技转发自小弟的谷哥博客,如有侵权请联系作者删除
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70002045/viewspace-2792259/ ,如需转载,请注明出处,否则将追究法律责任。