Android实现圆形纯数字按钮
程序员文章站
2022-06-16 13:35:42
最近在搞一个数字拨号按键按钮,就想到使用gridview,但在实现中遇到点问题,在给数字键进行圆形状态键设置时发现一个问题,gridview中自带的原生选择矩形按钮总是存在...
最近在搞一个数字拨号按键按钮,就想到使用gridview,但在实现中遇到点问题,在给数字键进行圆形状态键设置时发现一个问题,gridview中自带的原生选择矩形按钮总是存在,于是就想到了让其状态背景设置成透明。
一、自定义按钮控件relativelayout
public class keyboardview extends relativelayout { context mcontext; private gridview gridview; private list<map<string, string>> datalist; public keyboardview(context context) { this(context, null); } public keyboardview(context context, attributeset attrs) { super(context, attrs); this.mcontext = context; view view = view.inflate(context, r.layout.layout_virtual_keyboard, null); datalist = new arraylist<>(); gridview = (gridview) view.findviewbyid(r.id.gv_keybord); initdatalist(); setupview(); addview(view); //添加view } public list<map<string, string>> getdatalist() { return datalist; } private void initdatalist() { //初始化数字数据 for (int i = 1; i < 13; i++) { map<string, string> map = new hashmap<>(); if (i < 10) { map.put("keyname", string.valueof(i)); } else if (i == 10) { map.put("keyname", "*"); } else if (i == 11) { map.put("keyname", string.valueof(0)); } else if (i == 12) { map.put("keyname", "#"); } datalist.add(map); } } public gridview getgridview() { return gridview; } private void setupview() { keyboardadapter keyboardadapter = new keyboardadapter(mcontext, datalist); gridview.setadapter(keyboardadapter); } }
填充的gridview布局其中android:listselector=”@android:color/transparent”设置gridview选择器背景为透明色就可以按照item设置的样式显示状态选择了
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:background="#3f51b5" android:layout_height="wrap_content"> <!-- 键盘 --> <gridview android:id="@+id/gv_keybord" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:listselector="@android:color/transparent" android:horizontalspacing="1dp" android:numcolumns="3" android:verticalspacing="1dp" /> </framelayout>
二、gridview适配器,其中填充的item的背景可以是ui切的选型图,也可以自己通过shape绘制出来
public class keyboardadapter extends baseadapter { private context mcontext; private list<map<string, string>> mdatalist; public keyboardadapter(context context, list<map<string, string>> datalist) { this.mcontext = context; this.mdatalist = datalist; } @override public int getcount() { return mdatalist.size(); } @override public object getitem(int position) { return mdatalist.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder viewholder; if (convertview == null) { convertview = view.inflate(mcontext, r.layout.grid_item_virtual_keyboard, null); viewholder = new viewholder(); viewholder.btnkey = (textview) convertview.findviewbyid(r.id.btn_keys); convertview.settag(viewholder); } else { viewholder = (viewholder) convertview.gettag(); } viewholder.btnkey.settext(mdatalist.get(position).get("keyname")); return convertview; } public final class viewholder { public textview btnkey; } }
适配器item布局
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <textview android:id="@+id/btn_keys" android:layout_width="60dp" android:layout_height="60dp" android:layout_centerinparent="true" android:gravity="center" android:background="@drawable/phone_bt_dial_num_selected" android:textcolor="#333333" android:textsize="32sp" /> </relativelayout>
三,在activity中调用,首先要禁止系统的按键的弹出
public class normalkeyboardactivity extends appcompatactivity { private keyboardview keyboardview; private gridview gridview; private list<map<string, string>> valuelist; private edittext textnum; private imagebutton phone_delete; private string amount; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_keyboard); initview(); valuelist = keyboardview.getdatalist(); phonedelete(); } private void phonedelete() { phone_delete.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { deletenum(); } }); } //删除键删除的数字 private void deletenum() { if (amount.length() > 0) { amount = amount.substring(0, amount.length() - 1); textnum.settext(amount); editable ea = textnum.gettext(); textnum.setselection(ea.length()); } } private void initview() { textnum = (edittext) findviewbyid(r.id.textamount); // 不调用系统键盘 if (android.os.build.version.sdk_int <= 10) { textnum.setinputtype(inputtype.type_null); } else { this.getwindow().setsoftinputmode( windowmanager.layoutparams.soft_input_state_always_hidden); try { class<edittext> cls = edittext.class; method setshowsoftinputonfocus; setshowsoftinputonfocus = cls.getmethod("setshowsoftinputonfocus", boolean.class); setshowsoftinputonfocus.setaccessible(true); setshowsoftinputonfocus.invoke(textnum, false); } catch (exception e) { e.printstacktrace(); } } keyboardview = (keyboardview) findviewbyid(r.id.virtualkeyboardview); phone_delete = (imagebutton) findviewbyid(r.id.phone_bt_del); gridview = keyboardview.getgridview(); gridview.setonitemclicklistener(onitemclicklistener); textnum.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { keyboardview.setfocusable(true); keyboardview.setfocusableintouchmode(true); keyboardview.setvisibility(view.visible); } }); } private adapterview.onitemclicklistener onitemclicklistener = new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int position, long l) { if (position < 11 && position != 9) { amount = textnum.gettext().tostring().trim(); amount = amount + valuelist.get(position).get("keyname"); textnum.settext(amount); editable ea = textnum.gettext(); textnum.setselection(ea.length()); } else { if (position == 9) { amount = textnum.gettext().tostring().trim(); amount = amount + valuelist.get(position).get("keyname"); textnum.settext(amount); editable ea = textnum.gettext(); textnum.setselection(ea.length()); }else if (position == 11) { string amount = textnum.gettext().tostring().trim(); amount = amount + valuelist.get(position).get("keyname"); textnum.settext(amount); editable ea = textnum.gettext(); textnum.setselection(ea.length()); } } } }; }
activity中的布局
<?xml version="1.0" encoding="utf-8"?> <linearlayout 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:orientation="vertical"> <framelayout android:layout_width="match_parent" android:layout_height="50dp"> <edittext android:id="@+id/textamount" android:layout_width="match_parent" android:layout_height="match_parent" android:inputtype="numberdecimal" android:padding="14dp" android:gravity="center_horizontal" android:textcolor="#333333" android:textsize="34sp" /> <imagebutton android:id="@+id/phone_bt_del" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|center_vertical" android:src="@drawable/phone_bt_del_num_selected" /> </framelayout> <linearlayout android:layout_width="match_parent" android:orientation="vertical" android:layout_marginright="80dp" android:layout_marginleft="80dp" android:layout_height="wrap_content"> <com.ts.widget.keyboardview android:id="@+id/keyboardview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <imagebutton android:layout_width="match_parent" android:layout_height="60dp" android:background="#00ec69" android:src="@drawable/phone_icon_answer" android:text="拨号" /> </linearlayout> </linearlayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。