Android realizes the shaking cancellation input function of iPhone, and Android imitates the shaking function of wechat

In many programs, we may enter long text content, such as text messages and notes. If we want to cancel all the typed content at one time, many mobile phones need to press and hold the backspace key to delete word by word, which is a little troublesome. However, on the iPhone, there is a personalized function. When we want to cancel all the content we just entered, we can shake the mobile phone gently, A prompt box will pop up. Click OK to clear the content, as shown in the following figure:

In Android, it seems that most mobile phones do not customize this function, but we can implement this function ourselves. It is placed in our project program to reflect a more humanized design. The idea is very simple. It mainly uses the built-in acceleration sensor device of the mobile phone. In fact, we will think of the "shake" function of wechat, Personally, I think this function should be implemented in this way. When we input incorrectly and want to cancel all the input contents, we can shake our device, pop up a custom alertdialog, and complete the corresponding clearing operation according to the click event of the button.

First, we define an alertdialog ourselves and write a layout according to our personal design. Then, we create an alertdialog in the code and load the written layout file with layoutinflator

When the dialog box pops up, we hope that clicking the blank space outside the box will not make the dialog box disappear. We can set the following properties:

Then you can display the dialog box and define its size and other properties:

Secondly, we need to know how to use acceleration sensors:

1. Obtain relevant services of the system. All sensors must be accessed through sensormanager. Sensormanager = (sensormanager) getsystemservice (sensor_service);

2. Obtain the corresponding sensor type object through the sensormanager object. In this example, the acceleration sensor is used, and its type is type_ ACCELEROMETER, sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

3. Create a listener of sensoreventlistener object to monitor sensor events, mainly overriding the onsensorchanged method.

4. Register listening events in onresume. During registration, there will be three parameters: listener, sensor and sensitivity rate, of which there are four kinds of sensitivity: sensor_ DELAY_ Fastest: the most sensitive and responsive sensor_ DELAY_ Game: from the name, we can see the frequency sensor that will be used in most games_ DELAY_ Normal: generally, the frequency of use is relatively slow, which is applicable to most applications sensor_ DELAY_ UI: use the sensor to update the data in the UI and use this value

5. Unregister listening events in onpause

When overriding the onsensorchanged method, use an instance of sensorevent to get a series of values

The range of each value is between - 10 ~ 10. We can achieve the desired effect by judging the values in all directions, that is, when the values in all directions meet certain conditions, we can trigger our expected events

PS: in order to avoid the pop-up window caused by shaking when there is no input, or the pop-up window caused by shaking after the dialog box has popped up, we can use a self-defined flag bit to control it

The following is the main code part and the effect diagram after implementation

Click "cancel typing" to clear the text!

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>