In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to make a digital soft keyboard in the form of a dialog box by Android". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to make a digital soft keyboard in the form of a dialog box by Android.
I. introduction
Most of Android's custom soft keyboards are mainly realized through android's own control KeyboardView.
So, is there any other easy-to-use way to make a soft keyboard?
Dangdangdang!
Here we are going to talk about the soft keyboard in the form of dialog box, which is simple and easy to understand!
Next, through the custom digital soft keyboard to introduce how to use the dialog box to achieve a custom soft keyboard!
Let's see how it works!
II. Layout compilation
Let's look at the layout first!
The first is the implementation of activity_main:
Here, the layout of the main interface is very simple, only two TextView text boxes are needed!
Attention!
Why not use the EditView control to implement the input box?
We know that TextView belongs to the text control, and EditView belongs to the input box control, so we should choose the EditView input box control.
But!
Clicking the EditView input box will automatically jump out of the input method that comes with the system. This effect will affect the pop-up of our custom soft keyboard. So we also need to consider how to disable the keyboard that pops up automatically. Of course, this is not difficult to achieve, just add to the onCreate:
Tv_shownumber.setFocusable (false)
You can disable the system keyboard!
Here, use the TextView text box for data entry!
Stop talking! Lord layout!
Activity_main.xml
Android:onClick= "show" means that the method show will be called after clicking on the text box!
Next is to customize the soft keyboard layout!
Key.xml
The code is long, but it sums up 13 keystrokes and a TextView text box. I don't think it's difficult to think so in an instant!
This is the end of the layout code!
If you want your keyboard to look good, click to have a color change effect, or you can style it!
This is what the button looks like without skin:
This is what the button looks like with skin:
There's still a big difference!
The code of the button style is attached below:
Keyboard_btn.xml
Of course, not only the keyboard buttons need skin, but also the keyboard itself!
This is what the keyboard looks like without skin:
This is what the keyboard looks like with skin:
Here is the keyboard-style code:
DialogStyle.xml
# AFE1B1 @ null true @ null
Here, floating on the Activity settings is very important!
It changes the width of our entire keyboard!
With regard to the location of xml files, we generally put activity_main.xml and key.xml under the layout folder, keyboard_btn.xml under the drawable folder, and DialogStyle.xml under the values folder.
III. Logical compilation
With a good-looking skin, naturally also need an interesting soul!
Here it comes! here it comes!
MainActivity
Package com.example.mydialog; import androidx.appcompat.app.AlertDialog;import androidx.appcompat.app.AppCompatActivity; import android.app.Dialog;import android.os.Bundle;import android.view.Display;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.widget.Button;import android.widget.EditText;import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener {private TextView tv_shownumber;// private EditText tv_shownumber Private String shownumber; private Dialog dialog; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); tv_shownumber = findViewById (R.id.tv_shownumber) } private String num [] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "X"}; private String data = ""; @ Override public void onClick (View v) {shownumber = tv_shownumber.getText () .toString () Switch (v.getId ()) {case R.id.btn_0: data + = num [0]; tv_shownumber.setText (data); tv_show.setText (data); break; case R.id.btn_1: data + = num [1] Tv_shownumber.setText (data); tv_show.setText (data); break; case R.id.btn_2: data + = num [2]; tv_shownumber.setText (data); tv_show.setText (data); break Case R.id.btn_3: data + = num [3]; tv_shownumber.setText (data); tv_show.setText (data); break; case R.id.btn_4: data + = num [4]; tv_shownumber.setText (data) Tv_show.setText (data); break; case R.id.btn_5: data + = num [5]; tv_shownumber.setText (data); tv_show.setText (data); break; case R.id.btn_6: data + = num [6] Tv_shownumber.setText (data); tv_show.setText (data); break; case R.id.btn_7: data + = num [7]; tv_shownumber.setText (data); tv_show.setText (data); break Case R.id.btn_8: data + = num [8]; tv_shownumber.setText (data); tv_show.setText (data); break; case R.id.btn_9: data + = num [9]; tv_shownumber.setText (data) Tv_show.setText (data); break; case R.id.btn_X: data + = num [10]; tv_shownumber.setText (data); tv_show.setText (data); break; case R.id.btn_yes: dialog.dismiss () Break; case R.id.btn_del: data = data.substring (0, data.length ()-1); tv_shownumber.setText (data); tv_show.setText (data); break; default: break;}} private View inflate Private Button btn_0; private Button btn_1; private Button btn_2; private Button btn_3; private Button btn_4; private Button btn_5; private Button btn_6; private Button btn_7; private Button btn_8; private Button btn_9; private Button btn_X; private Button btn_yes; private Button btn_del; private TextView tv_show Public void show (View view) {dialog = new Dialog (this,R.style.DialogStyle); inflate = LayoutInflater.from (this) .propagate (R.layout.key, null); / / dynamically add layout btn_0 = inflate.findViewById (R.id.btn_0); btn_1 = inflate.findViewById (R.id.btn_1); btn_2 = inflate.findViewById (R.id.btn_2) Btn_3 = inflate.findViewById (R.id.btn_3); btn_4 = inflate.findViewById (R.id.btn_4); btn_5 = inflate.findViewById (R.id.btn_5); btn_6 = inflate.findViewById (R.id.btn_6); btn_7 = inflate.findViewById (R.id.btn_7); btn_8 = inflate.findViewById (R.id.btn_8) Btn_9 = inflate.findViewById (R.id.btn_9); btn_X = inflate.findViewById (R.id.btn_X); btn_yes = inflate.findViewById (R.id.btn_yes); btn_del = inflate.findViewById (R.id.btn_del); tv_show = inflate.findViewById (R.id.tv_show); btn_1.setOnClickListener (this); btn_2.setOnClickListener (this) Btn_3.setOnClickListener (this); btn_4.setOnClickListener (this); btn_5.setOnClickListener (this); btn_6.setOnClickListener (this); btn_7.setOnClickListener (this); btn_8.setOnClickListener (this); btn_9.setOnClickListener (this); btn_X.setOnClickListener (this); btn_0.setOnClickListener (this); btn_yes.setOnClickListener (this) Btn_del.setOnClickListener (this); dialog.setContentView (inflate); Window dialogWindow = dialog.getWindow (); WindowManager m = getWindowManager (); Display d = m.getDefaultDisplay (); / / get screen width, high-end WindowManager.LayoutParams p = dialogWindow.getAttributes (); / / get the current parameter value of the dialog box p.dimAmount = 0f / / set background transparency dialogWindow.setGravity (Gravity.BOTTOM); p.width = d.getWidth (); / / set keyboard width dialogWindow.setAttributes (p); dialog.show ();}}
The function is set for each key in the main code, and the dynamic layout key.xml is introduced, and the show () method is implemented to let the custom keyboard pop up in the form of a dialog box!
Here, the background transparency setting can make the appearance of the keyboard smoother, and the setting of this item can also be done in xml!
DialogWindow.setGravity (Gravity.BOTTOM)
The purpose of this sentence is to place our keyboard at the bottom of the page.
At this point, I believe you have a deeper understanding of "how to make a digital soft keyboard in the form of a dialog box by Android". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.