Android uses activity to implement a simple input dialog box

1. Demand analysis

As we all know, there are always such comments in applications. Some applications directly add an EditText and a button at the bottom to let users input text or expression and click the button to submit; Although some also have EditText, it is only a "decoration" and does not have the input function. After users click it, a pop-up will pop up to a page that can be really edited or a dialog box that can input content.

For example, the following effects:

The effect here can be subdivided into four points:

At first, I thought of popupwindow, but because there was EditText in it, it was a headache to interact with the soft keyboard, so I used activity instead. In this way, we can use the dialog box like activity, which is much more convenient. However, after all, it is different from the activity we usually use. In particular, we should set its style, otherwise it is also a pile of pits.

2. Layout and style of dialog activity

Let's start to implement the dialog we want. For a new project, mainactivity is just a supporting role. Just put a button at the bottom. Our protagonist is dialogactivity. Its layout is very simple, just like the usual activity:

Focus on its style. See the following code:

There are many attributes to be set. I have made comments. Just understand the role of each attribute. Let's talk about it in detail here. Don't forget to use this topic for dialogactivity in the manifest file:

Run it, I believe you can see the effect.

3. Auto pop up soft keyboard effect

We have made the dialog interface, but for better user experience, we need to automatically pop up the soft keyboard when the dialog box appears. Two methods are described below:

3.1. Use inputmethodmanager class to display soft keyboard

We usually want to make an EditText get the focus and automatically pop up the soft keyboard, which can be written as follows:

But there is one thing to note: if we want EditText to get the focus, we must wait until the interface is drawn. Therefore, a delay of 300ms is set to execute the code of pop-up soft keyboard, leaving time for drawing the interface:

With the above code, you can pop up the soft keyboard by yourself.

3.1. Set windowsoftinputmode attribute

If you are careful, you must find that there is a property of windowsoftinputmode in the code that sets the style for dialogactivity. Please forgive me for selling it. This property sets the interaction mode between the window and the soft keyboard. It has many attributes. You can refer to the reference articles given later. Here we use adjustresize, which is used to adjust the interface layout to leave enough space for the soft keyboard. What about statehidden? In fact, it is the ghost that the soft keyboard does not pop up automatically. It means that in general, the soft keyboard is hidden. We change it to another attribute: statevisible, which indicates that the soft keyboard is usually visible.

Run it again, and the soft keyboard will arrive as scheduled.

4. Postscript

The effect we mentioned in the requirements analysis has been realized. Later, I also thought about adding custom animation effects to the dialog box, but the animation when exiting has never been set successfully, so if any readers have realized it, welcome to communicate and learn.

I saved the source code to the code cloud. If necessary, you can refer to: you can enter the source code of the dialog box

You can also download it locally: click here

Well, the above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

5. Reference articles

Detailed explanation of Android: windowsoftinputmode attribute

Thoroughly solve the common problems of soft keyboard in Android development

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>