Android实现圆形纯数字按钮

2019-07-31 09:57| 发布者: |

最近在搞一个数字拨号按键按钮,就想到使用gridview,但在实现中遇到点问题,在给数字键进行圆形状态键设置时发现一个问题,gridview中自带的原生选择矩形按钮总是存在,于是就想到了让其状态背景设置成透明。


一、自定义按钮控件relativelayout

public class keyboardview extends relativelayout {
 context mcontext;
 private gridview gridview;
 private list map string, string datalist;
 public keyboardview {
 this;
 public keyboardview {
 super;
 this.mcontext = context;
 view view = view.inflate;
 datalist = new arraylist ;
 gridview =  view.findviewbyid;
 initdatalist;
 setupview;
 addview; //添加view
 public list map string, string getdatalist {
 return datalist;
 private void initdatalist { //初始化数字数据
 for  {
 map string, string map = new hashmap ;
 if  {
 map.put);
 } else if  {
 map.put;
 } else if  {
 map.put);
 } else if  {
 map.put;
 datalist.add;
 public gridview getgridview {
 return gridview;
 private void setupview {
 keyboardadapter keyboardadapter = new keyboardadapter;
 gridview.setadapter;
}

填充的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 {
 this.mcontext = context;
 this.mdatalist = datalist;
 @override
 public int getcount {
 return mdatalist.size;
 @override
 public object getitem {
 return mdatalist.get;
 @override
 public long getitemid {
 return position;
 @override
 public view getview {
 viewholder viewholder;
 if  {
 convertview = view.inflate;
 viewholder = new viewholder;
 viewholder.btnkey =  convertview.findviewbyid;
 convertview.settag;
 } else {
 viewholder =  convertview.gettag;
 viewholder.btnkey.settext.get);
 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 {
 super.oncreate;
 setcontentview;
 initview;
 valuelist = keyboardview.getdatalist;
 phonedelete;
 private void phonedelete {
 phone_delete.setonclicklistener {
 @override
 public void onclick {
 deletenum;
 //删除键删除的数字
 private void deletenum {
 if  0) {
 amount = amount.substring - 1);
 textnum.settext;
 editable ea = textnum.gettext;
 textnum.setselection);
 private void initview {
 textnum =  findviewbyid;
 // 不调用系统键盘
 if  {
 textnum.setinputtype;
 } else {
 this.getwindow.setsoftinputmode;
 try {
 class edittext cls = edittext.class;
 method setshowsoftinputonfocus;
 setshowsoftinputonfocus = cls.getmethod;
 setshowsoftinputonfocus.setaccessible;
 setshowsoftinputonfocus.invoke;
 } catch  {
 e.printstacktrace;
 keyboardview =  findviewbyid;
 phone_delete =  findviewbyid;
 gridview = keyboardview.getgridview;
 gridview.setonitemclicklistener;
 textnum.setonclicklistener {
 @override
 public void onclick {
 keyboardview.setfocusable;
 keyboardview.setfocusableintouchmode;
 keyboardview.setvisibility;
 private adapterview.onitemclicklistener onitemclicklistener = new adapterview.onitemclicklistener {
 @override
 public void onitemclick {
 if  {
 amount = textnum.gettext.tostring.trim;
 amount = amount + valuelist.get.get;
 textnum.settext;
 editable ea = textnum.gettext;
 textnum.setselection);
 } else {
 if  {
 amount = textnum.gettext.tostring.trim;
 amount = amount + valuelist.get.get;
 textnum.settext;
 editable ea = textnum.gettext;
 textnum.setselection);
 }else if  {
 string amount = textnum.gettext.tostring.trim;
 amount = amount + valuelist.get.get;
 textnum.settext;
 editable ea = textnum.gettext;
 textnum.setselection);
}

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 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

<
>
关于我们
AB模版网成立于2014年,我们是一家专注用户体验设计开发与互联网品牌建设的设计公司,创立至今为2000多位客户提供了创新与专业的设计方案。设计服务范围包括:交互原型设计、产品视觉设计、网站设计与开发建设、移动及软件产品界面设计、图标设计、品牌及平面设计等。

联系我们

13588889999服务时间:9:00-18:00)

admin@adminbuy.cn

官方微信官方微信

部门热线

前   台:13588889999
业务部:13588889999
客服部:13588889999
技术部:13566667777
人事部:13566667777

咨询电话13588889999 返回顶部
返回顶部