Android实现圆形纯数字按钮

 更新时间:2019年02月01日 08:44:08   作者:WhatYouSeeMe  
这篇文章主要为大家详细介绍了Android实现圆形纯数字按钮,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

最近在搞一个数字拨号按键按钮,就想到使用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>

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

相关文章

  • Android开发实现ImageView加载摄像头拍摄的大图功能

    Android开发实现ImageView加载摄像头拍摄的大图功能

    这篇文章主要介绍了Android开发实现ImageView加载摄像头拍摄的大图功能,涉及Android基于ImageView的摄像头拍摄图片加载、保存及权限控制等相关操作技巧,需要的朋友可以参考下
    2017-11-11
  • android自定义WaveView水波纹控件

    android自定义WaveView水波纹控件

    这篇文章主要为大家详细介绍了android自定义WaveView水波纹控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • Android实现调用手机摄像头录像限制录像时长

    Android实现调用手机摄像头录像限制录像时长

    这篇文章主要为大家详细介绍了Android实现调用手机摄像头录像限制录像时长,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Android+SQLite数据库实现的生词记事本功能实例

    Android+SQLite数据库实现的生词记事本功能实例

    这篇文章主要介绍了Android+SQLite数据库实现的生词记事本功能,结合具体实例形式分析了Android操作SQLite数据库实现生词记录功能的操作步骤与相关注意事项,需要的朋友可以参考下
    2017-09-09
  • android设备不识别awk命令 缺少busybox怎么办

    android设备不识别awk命令 缺少busybox怎么办

    这篇文章主要为大家详细介绍了android设备不识别awk命令,缺少busybox的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-04-04
  • Android RIL使用详解

    Android RIL使用详解

    这篇文章主要介绍了Android RIL使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-07-07
  • Fragment通过FragmentManager实现通信功能详细讲解

    Fragment通过FragmentManager实现通信功能详细讲解

    这篇文章主要介绍了Fragment通过FragmentManager实现通信功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2023-01-01
  • Android中RecyclerView实现多级折叠列表效果(二)

    Android中RecyclerView实现多级折叠列表效果(二)

    这篇文章主要给大家介绍了Android中RecyclerView实现多级折叠列表的相关资料,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-05-05
  • Android弹出窗口实现方法

    Android弹出窗口实现方法

    这篇文章主要介绍了Android弹出窗口实现方法,涉及Android TextView及鼠标事件的响应相关技巧,需要的朋友可以参考下
    2016-01-01
  • Android Studio3.0升级后使用注意事项及解决方法

    Android Studio3.0升级后使用注意事项及解决方法

    这篇文章主要介绍了Android Studio3.0升级后使用注意事项及解决方法,需要的朋友参考下吧
    2017-12-12

最新评论