android自定义密码输入键盘控件
2018-01-22 15:00
441 查看
记录贴
一个组合控件
按键item布局文件
自定义组合控件的布局文件
控件代码
布局文件里调用
效果图
有需要的人可以自行添加方法,ui方面自己调整,这只是简单的实现,还需要很多修改,大神勿喷!
一个组合控件
按键item布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" > <TextView android:id="@+id/tv_softText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textColor="#000" android:textSize="30sp" android:text="2" /> </RelativeLayout>
自定义组合控件的布局文件
<?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" > <GridView android:id="@+id/gv_password" android:layout_width="match_parent" android:layout_height="45dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:numColumns="6" android:stretchMode="columnWidth" android:verticalSpacing="1dp" android:horizontalSpacing="1dp" android:background="#e2e2e2" > </GridView> <GridView android:id="@+id/gv_gridView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:numColumns="3" android:stretchMode="columnWidth" android:verticalSpacing="1dp" android:horizontalSpacing="1dp" android:background="#e2e2e2" > </GridView> </LinearLayout>
控件代码
public class SoftInput extends RelativeLayout { private Context context; private String[] s=new String[]{ "0","1","2","3","4","5","6","7","8","","9","C" }; private List<String> clickResult=new ArrayList<>(); private GridView password,gridView; private GideAdapter adapter; private GideAdapter adapter2; public interface OnSoftItemClickListener{ void OnClick(String value,int position); } private OnSoftItemClickListener onSoftItemClickListener; public void setOnSoftItemClickListener(OnSoftItemClickListener onSoftItemClickListener){ this.onSoftItemClickListener=onSoftItemClickListener; } public SoftInput(Context context) { this(context,null); } public SoftInput(Context context, AttributeSet attrs) { this(context, attrs,0); } public SoftInput(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context=context; LayoutInflater.from(context).inflate(R.layout.view_soft_input, this, true); gridView= (GridView) findViewById(R.id.gv_gridView); password= (GridView) findViewById(R.id.gv_password); adapter=new GideAdapter(context,s); gridView.setAdapter(adapter); gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (onSoftItemClickListener!=null){ onSoftItemClickListener.OnClick(adapter.getItemValue(position),position); } if (position<9||position==10){ setClickResult(s[position]); }else if (position==s.length-1){ removeClickResult(); } } }); } private void setClickResult(String s){ if (clickResult!=null&&clickResult.size()<6){ clickResult.add(s); setPasswordAdapter(); } } private void removeClickResult(){ if (clickResult!=null&&clickResult.size()>0){ clickResult.remove(clickResult.size()-1); setPasswordAdapter(); } } private void setPasswordAdapter(){ String[] p=clickResult.toArray(new String[clickResult.size()]); adapter2=new GideAdapter(context,p); password.setAdapter(adapter2); } public List<Integer> getPassword(){ List<Integer> pd=new ArrayList<>(); if (clickResult!=null&&clickResult.size()>0){ for (int i=0;i<clickResult.size();i++){ pd.add(Integer.parseInt(clickResult.get(i))); } return pd; } return null; } class GideAdapter extends BaseAdapter{ private Context context; private String[] s; public GideAdapter(Context context, String[] s) { this.context = context; this.s = s; } private String getItemValue(int position){ if (s!=null){ return s[position]; } return ""; } @Override public int getCount() { return s.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { Holder holder; if (convertView==null){ holder=new Holder(); convertView= LayoutInflater.from(context).inflate(R.layout.item_soft_input,parent,false); holder.tv= (TextView) convertView.findViewById(R.id.tv_softText); convertView.setTag(holder); }else { holder= (Holder) convertView.getTag(); } holder.tv.setText(s[position]); return convertView; } class Holder { TextView tv; } } }
布局文件里调用
<com.example.csd.shop.view.SoftInput android:id="@+id/sfi_soft" android:layout_width="match_parent" android:layout_height="wrap_content"> </com.example.csd.shop.view.SoftInput>
效果图
有需要的人可以自行添加方法,ui方面自己调整,这只是简单的实现,还需要很多修改,大神勿喷!
相关文章推荐
- Android 自定义的数字键盘 支持随意拖动 和稳定的字符输入的控件
- iOS无法使用自定义密码输入控件(自带键盘)
- Android 为app自定义一个输入密码键盘
- iOS无法使用自定义密码输入控件(自带键盘)
- Android 自定义键盘控件_身份证号码输入
- iOS无法使用自定义密码输入控件(自带键盘)
- Android 平板中 自定义键盘(popuwindow) 居于屏幕左下方 仿微信的密码输入界面
- Android自定义类似支付宝密码输入的控件
- Android自定义类似支付宝密码输入的控件
- Android自定义控件之电话拨打小键盘
- Android仿支付宝淘宝 - 自定义密码输入框和键盘
- ANDROID 输入法出现挤压屏幕、ANDROID输入键盘覆盖了屏幕控件的解决办法
- Android自定义控件之我的电话小键盘
- Android自定义密码键盘
- android自定义键盘 限制车牌号码输入
- Android Fragment 中自定义键盘如何调整布局,不挡住界面控件
- Android高效率实现自定义密码输入效果,仿微信和支付宝密码输入效果
- 自定义View之自定义支付宝密码输入控件
- android 自定义纯数字键盘输入
- 简易密码输入自定义控件示例 含自定义事件 可在调用窗口事件视图中看到事件名