Android怎么实现圆形纯数字按钮
这篇文章主要介绍了Android怎么实现圆形纯数字按钮,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
为河间等地区用户提供了全套网页设计制作服务,及河间网站建设行业解决方案。主营业务为网站制作、网站设计、河间网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
最近在搞一个数字拨号按键按钮,就想到使用GridView,但在实现中遇到点问题,在给数字键进行圆形状态键设置时发现一个问题,GridView中自带的原生选择矩形按钮总是存在,于是就想到了让其状态背景设置成透明。
一、自定义按钮控件RelativeLayout
public class KeyboardView extends RelativeLayout { Context mContext; private GridView gridView; private List
填充的GridView布局其中android:listSelector=”@android:color/transparent”设置GridView选择器背景为透明色就可以按照item设置的样式显示状态选择了
二、GridView适配器,其中填充的item的背景可以是UI切的选型图,也可以自己通过shape绘制出来
public class KeyBoardAdapter extends BaseAdapter { private Context mContext; private List> mDataList; public KeyBoardAdapter(Context context, List > 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布局
三,在Activity中调用,首先要禁止系统的按键的弹出
public class NormalKeyBoardActivity extends AppCompatActivity { private KeyboardView keyboardView; private GridView gridView; private List> 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 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中的布局
感谢你能够认真阅读完这篇文章,希望小编分享的“Android怎么实现圆形纯数字按钮”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!
网页题目:Android怎么实现圆形纯数字按钮
网站URL:http://ybzwz.com/article/gosise.html