In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to customize a numeric keyboard in Android". In daily operation, I believe many people have doubts about how to customize a numeric keyboard in Android. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to customize a numeric keyboard in Android". Next, please follow the editor to study!
1. Realize the xml layout of the keyboard
Grid-style layouts can be implemented with GridView or RecyclerView, and its practical GridView is more convenient, but I chose to use RecyclerView here in order to be more familiar with the use of RecyclerView.
RecyclerView is used to implement the keyboard layout, and the above RelativeLayout is to realize the click event of putting the keyboard away.
two。 Implement keyboard layout in code, fill in data, and add click events
Our new class KeyboardView inherits from RelativeLayout, correlates the layout file above, and then does some initialization operations: populating the data for RecyclerView, setting adapters, animating the appearance and disappearance, writing some methods that will be used, and so on.
Public class KeyboardView extends RelativeLayout {private RelativeLayout rlBack; private RecyclerView recyclerView; private List datas; private KeyboardAdapter adapter; private Animation animationIn; private Animation animationOut; public KeyboardView (Context context) {this (context, null);} public KeyboardView (Context context, AttributeSet attrs) {this (context, attrs, 0);} public KeyboardView (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); init (context, attrs, defStyleAttr) } private void init (Context context, AttributeSet attrs, int defStyleAttr) {LayoutInflater.from (context) .propagate (R.layout.layout_key_board, this); rlBack = findViewById (R.id.rl_back); rlBack.setOnClickListener (new OnClickListener () {@ Override public void onClick (View view) {/ / Click to close the keyboard dismiss ();}}); recyclerView = findViewById (R.id.recycler_view) InitData (); initView (); initAnimation ();} / / populate the data private void initData () {datas = new ArrayList (); for (int I = 0; I < 12; iTunes +) {if (I < 9) {datas.add (String.valueOf (I + 1));} else if (I = 9) {datas.add (".") } else if (I = = 10) {datas.add ("0");} else {datas.add ("");} / / set adapter private void initView () {recyclerView.setLayoutManager (new GridLayoutManager (getContext (), 3)); adapter = new KeyboardAdapter (getContext (), datas); recyclerView.setAdapter (adapter) } / / initialize the animation private void initAnimation () {animationIn = AnimationUtils.loadAnimation (getContext (), R.anim.keyboard_in); animationOut = AnimationUtils.loadAnimation (getContext (), R.anim.keyboard_out);} / / pop-up soft keyboard public void show () {startAnimation (animationIn); setVisibility (VISIBLE) } / / close the soft keyboard public void dismiss () {if (isVisible ()) {startAnimation (animationOut); setVisibility (GONE);}} / / determine the status of the soft keyboard public boolean isVisible () {if (getVisibility () = = VISIBLE) {return true;} return false;} public void setOnKeyBoardClickListener (KeyboardAdapter.OnKeyboardClickListener listener) {adapter.setOnKeyboardClickListener (listener) } public List getDatas () {return datas;} public RelativeLayout getRlBack () {return rlBack;}}
Adapter is full of very simple code, so it will not be posted here. I will give the download address of the source code at the end of the article.
So far, the custom numeric keypad is basically written, but the most important thing is to be used in conjunction with Edittext.
3. Used in conjunction with Edittext
1. Disable the system soft keyboard
If (Build.VERSION.SDK_INT 0) {etInput.setText (num.substring (0, num.length ()-1); etInput.setSelection (etInput.getText (). Length ());} at this point, the study on "how to customize a numeric keyboard in Android" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.