写此功能主要是因为朋友有个需求是选择单个支付方式,so......代码很简单,就不做多余的解释,如下就是实现详情:
首先是布局文件:
activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f5f5f5"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#222222"
android:gravity="center"
android:text="RadioButtonDemo"
android:textColor="#ffffff"
android:textSize="18sp" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_margin="5dp"
android:background="@drawable/shape_white"
android:divider="#a9a9a9"
android:dividerHeight="0.5dp" >
</ListView>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/listView1"
android:layout_centerHorizontal="true"
android:layout_margin="15dp"
android:text="提交" />
</RelativeLayout>
item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:drawableLeft="@drawable/wechat"
android:drawablePadding="10dp"
android:gravity="center_vertical"
android:text="微信支付"
android:textColor="@android:color/black"
android:textSize="16sp" />
<CheckBox
android:id="@+id/checkBox"
style="@style/CustomCheckboxTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp" />
</RelativeLayout>
</LinearLayout>
自定义CheckBox样式
CustomCheckboxTheme:
<style name="CustomCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/checkbox_style</item>
</style>
checkbox_style:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/checkbox_pressed" android:state_checked="true"/>
<item android:drawable="@drawable/checkbox_normal" android:state_checked="false"/>
<item android:drawable="@drawable/checkbox_normal"/>
</selector>
MainActivity:
package com.fangs.rb;
import java.util.ArrayList;
import java.util.List;
import com.fangs.rb.adapter.CustomAdpter;
import com.fangs.rb.bean.Items;
import com.fangs.rb.bean.utils.PreferencesUtil;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private ListView listView;
private CustomAdpter adapter;
private int[] icons = { R.drawable.wechat, R.drawable.alipay };
private String[] names = { "微信支付", "支付宝支付" };
private String[] payways = { "wechat", "alipay" };
private boolean[] checked = { false, false };
private List<Items> itemStatus = new ArrayList<Items>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
for (int i = 0; i < icons.length; i++) {
Items status = new Items();
status.setIcon(icons[i]);
status.setName(names[i]);
status.setChecked(checked[i]);
status.setPayway(payways[i]);
itemStatus.add(status);
}
adapter = new CustomAdpter(this, itemStatus);
listView.setAdapter(adapter);
findViewById(R.id.button).setOnClickListener(this);
}
@Override
public void onClick(View v) {
String way = PreferencesUtil.getStringPreferences(this, "pay_way");
if (way != null && !TextUtils.isEmpty(way)) {
if (way.equals("wechat")) {
// 微信支付
Toast.makeText(this, "微信支付", Toast.LENGTH_SHORT).show();
} else {
// 支付宝支付
Toast.makeText(this, "支付宝支付", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "请选择支付方式", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
PreferencesUtil.clearPreferences(this, "pay_way");
}
}
CustomAdapter:
package com.fangs.rb.adapter;
import java.util.List;
import com.fangs.rb.R;
import com.fangs.rb.bean.Items;
import com.fangs.rb.bean.utils.PreferencesUtil;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class CustomAdpter extends BaseAdapter implements OnClickListener {
private List<Items> items;
private Context mContext;
public CustomAdpter(Context context, List<Items> itemStatus) {
this.mContext = context;
this.items = itemStatus;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int arg0) {
return null;
}
@Override
public long getItemId(int arg0) {
return 0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ViewHolder h = null;
if (arg1 == null) {
arg1 = LayoutInflater.from(mContext).inflate(R.layout.item, null);
h = new ViewHolder();
h.name = (TextView) arg1.findViewById(R.id.name);
h.checkBox = (CheckBox) arg1.findViewById(R.id.checkBox);
h.checkBox.setOnClickListener(this);
arg1.setTag(h);
} else {
h = (ViewHolder) arg1.getTag();
}
Drawable drawable = mContext.getResources().getDrawable(items.get(arg0).getIcon());
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
h.name.setCompoundDrawables(drawable, null, null, null);
h.name.setText(items.get(arg0).getName());
h.checkBox.setChecked(items.get(arg0).isChecked());
h.checkBox.setTag(arg0);
return arg1;
}
@Override
public void onClick(View v) {
int position = (Integer) v.getTag();
for (int i = 0; i < items.size(); i++) {
items.get(i).setChecked(false);
}
items.get(position).setChecked(true);
PreferencesUtil.setPreferences(mContext, "pay_way", items.get(position).getPayway());
notifyDataSetChanged();
}
static class ViewHolder {
TextView name;
CheckBox checkBox;
}
}
Items:
package com.fangs.rb.bean;
public class Items {
private boolean checked;
private int icon;
private String name;
private String payway;
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String getPayway() {
return payway;
}
public void setPayway(String payway) {
this.payway = payway;
}
}
PreferencesUtil:
package com.fangs.rb.bean.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
/**
* SharePreference宸ュ叿绫?
*
* @author Sanpark
*
*/
public class PreferencesUtil {
private static String PreferenceName = "FangsConstant";
/**
* SharePreference中的值
*/
public static String getStringPreferences(Context context, String name) {
SharedPreferences sp = context.getSharedPreferences(PreferenceName, Context.MODE_PRIVATE);
// 获取数据
return sp.getString(name, "");
}
/**
* 将String信息存入Preferences
*
* @param context
* @param userName
*/
public static void setPreferences(Context context, String name, String value) {
SharedPreferences sp = context.getSharedPreferences(PreferenceName, Context.MODE_PRIVATE);
// 存入数据
Editor editor = sp.edit();
editor.putString(name, value);
editor.commit();
}
public static void clearPreferences(Context context, String key) {
SharedPreferences sp = context.getSharedPreferences(PreferenceName, Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.remove(key);
editor.commit();
}
}