Android-Like iPhone shake-Undo Input Function (WeChat shake-shake function)

Source: Internet
Author: User

In many programs, we may enter long text content, such as text messages and notebooks. If you want to cancel all the typed content at one time, many mobile phones need to keep pressing the return key to delete it one by one, A little troublesome, but there is a user-friendly feature on the iPhone. When we want to cancel all the content we just entered, we can shake the phone and a prompt box will pop up, click OK to clear the content,

In android, general mobile phones do not seem to have customized this function, but we can implement this function on our own and place it in our project programs to reflect a more user-friendly design. The idea is very simple, this is mainly because of the built-in acceleration sensor device of the mobile phone. In fact, everyone will think of the "Shake" function. I personally think this function should be implemented in this way, when we mistakenly input and want to cancel all input content, shake our device, and a custom alertdialog will pop up. The corresponding clearing operation will be completed based on the Click Event of the button.

First, we define an alertdialog, write a layout based on our own design, and then create an AlertDialog in the Code and load the prepared layout file using LayoutInflater.

AlertDialog. Builder builder = new AlertDialog. Builder (this );
Dialog = builder. create ();
LayoutInflater inflater = LayoutInflater. from (this );
LinearLayout layout = (LinearLayout) inflater. inflate (R. layout. alertdialog, null );

When the dialog box is displayed, we hope that the blank space outside the box will not disappear. We can set the following attributes:

Dialog. setCanceledOnTouchOutside (false );

Then, you can display the dialog box and define attributes such as the size:

Dialog. show ();
Dialog. setContentView (layout, new LayoutParams (400,250 ));

Bytes ------------------------------------------------------------------------------------

Next, we need to know how to use the acceleration sensor:

1. Obtain system-related services. All Sensors must be accessed through SensorMannager. sensorManager = (SensorManager) getSystemService (SENSOR_SERVICE );

2. Get the corresponding Sensor type object through the SensorManager object. In this example, acceleration Sensor is used, and its type is TYPE_ACCELEROMETER,

Sensor = sensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER );

3. Create a listener for the SensorEventListener object to monitor the Sensor event. The onSensorChanged method is mainly rewritten.

4. register the listener event in onResume. When registering the listener, there will be three parameters: listener, sensor, and sensitivity rate. There are four sensitivity parameters:

SENSOR_DELAY_FASTEST: the most sensitive and responsive

SENSOR_DELAY_GAME: the frequency that most games use.

SENSOR_DELAY_NORMAL: generally used at a low frequency, applicable to most applications

SENSOR_DELAY_UI: Use a sensor to update data in the UI.


5. Cancel listening event registration in onPause

Bytes ------------------------------------------------------------------------------------

When you override the onSensorChanged method, use the SensorEvent instance to obtain a series of values.

Float values [] = event. values;
Float x = values [0]; // acceleration of gravity in the x axis
Float y = values [1]; // gravity acceleration in the y axis
Float z = values [2]; // acceleration of gravity in the z axis

The range of each value is-10 ~ Between 10, we can determine the value of each direction to achieve the effect we need, that is, when the value in each direction meets certain conditions, it triggers our expected event

Bytes ------------------------------------------------------------------------------------

PS: To avoid shaking or pop-up windows when there is no input, or continue shaking after the pop-up dialog box appears, we can use a custom flag to control it.

The main code section and

[Java]
<SPAN style = "FONT-FAMILY: Comic Sans MS; FONT-SIZE: 18px"> package com. example. shakedemo;
 
Import android. hardware. Sensor;
Import android. hardware. SensorEvent;
Import android. hardware. SensorEventListener;
Import android. hardware. SensorManager;
Import android. OS. Bundle;
Import android. OS. Vibrator;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup. LayoutParams;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. LinearLayout;
Import android. app. Activity;
Import android. app. AlertDialog;
 
Public class MainA extends Activity {
 
Private SensorManager sensorManager;
Private Vibrator vibrator; // mobile phone Vibration
Private EditText txt_content;
Private Button btn_delete, btn_cancle;
Private AlertDialog dialog;
Private Sensor sensor;
Private boolean hasShaked = false; // identify whether the flag has been shaken
 
Private SensorEventListener listener = new SensorEventListener (){
 
@ Override
Public void onSensorChanged (SensorEvent event ){
// TODO Auto-generated method stub
Float values [] = event. values;
Float x = values [0]; // acceleration of gravity in the x axis
Float y = values [1]; // gravity acceleration in the y axis
Float z = values [2]; // acceleration of gravity in the z axis

// The threshold value set here is 18, which can be changed as needed.
Int medumValue = 18;
If (Math. abs (x)> medumValue | Math. abs (y)> medumValue | Math
. Abs (z)> medumValue) & hasShaked = false ){
If ((! (Txt_content.getText (). toString (). equals ("")))
& HasShaked = false ){
Vibrator. vibrate (200); // sets the vibration frequency.
ShowDialog ();
HasShaked = true;
}
}
}
 
@ Override
Public void onAccuracyChanged (Sensor sensor, int accuracy ){
// TODO Auto-generated method stub
 
}
};
 
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
SensorManager = (SensorManager) getSystemService (SENSOR_SERVICE );
Sensor = sensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER );
Vibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE );
Txt_content = (EditText) findViewById(R.id.txt _ content );
}
 
Private void showDialog (){
AlertDialog. Builder builder = new AlertDialog. Builder (this );
Dialog = builder. create ();
LayoutInflater inflater = LayoutInflater. from (this );
LinearLayout layout = (LinearLayout) inflater. inflate (
R. layout. alertdialog, null );
Dialog. setCanceledOnTouchOutside (false); // The blank area outside the click box does not make the dialog box disappear.
Dialog. show ();
Dialog. setContentView (layout, new LayoutParams (400,250 ));
 
Btn_delete = (Button) layout. findViewById (R. id. btn_delete );
Btn_delete.setOnClickListener (new OnClick ());
Btn_cancle = (Button) layout. findViewById (R. id. btn_cancle );
Btn_cancle.setOnClickListener (new OnClick ());
}
 
Class OnClick implements android. view. View. OnClickListener {
 
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Switch (v. getId ()){
Case R. id. btn_delete:
Txt_content.getText (). clear ();
Dialog. dismiss ();
HasShaked = false;
Break;
Case R. id. btn_cancle:
Dialog. dismiss ();
HasShaked = false;
Default:
Break;
}
}
 
}
 
@ Override
Protected void onResume (){
// TODO Auto-generated method stub
Super. onResume ();
// Register a listener event
If (sensorManager! = Null ){
SensorManager. registerListener (listener, sensor,
SensorManager. SENSOR_DELAY_NORMAL );
}
}
 
@ Override
Protected void onPause (){
// TODO Auto-generated method stub
Super. onPause ();
// Cancel the listener
If (sensorManager! = Null ){
SensorManager. unregisterListener (listener );
}
}
 
}
 
 
</SPAN>

Package com. example. shakedemo;

Import android. hardware. Sensor;
Import android. hardware. SensorEvent;
Import android. hardware. SensorEventListener;
Import android. hardware. SensorManager;
Import android. OS. Bundle;
Import android. OS. Vibrator;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup. LayoutParams;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. LinearLayout;
Import android. app. Activity;
Import android. app. AlertDialog;

Public class MainA extends Activity {

Private SensorManager sensorManager;
Private Vibrator vibrator; // mobile phone Vibration
Private EditText txt_content;
Private Button btn_delete, btn_cancle;
Private AlertDialog dialog;
Private Sensor sensor;
Private boolean hasShaked = false; // identify whether the flag has been shaken

Private SensorEventListener listener = new SensorEventListener (){

@ Override
Public void onSensorChanged (SensorEvent event ){
// TODO Auto-generated method stub
Float values [] = event. values;
Float x = values [0]; // acceleration of gravity in the x axis
Float y = values [1]; // gravity acceleration in the y axis
Float z = values [2]; // acceleration of gravity in the z axis

// The threshold value set here is 18, which can be changed as needed.
Int medumValue = 18;
If (Math. abs (x)> medumValue | Math. abs (y)> medumValue | Math
. Abs (z)> medumValue) & hasShaked = false ){
If ((! (Txt_content.getText (). toString (). equals ("")))
& HasShaked = false ){
Vibrator. vibrate (200); // sets the vibration frequency.
ShowDialog ();
HasShaked = true;
}
}
}

@ Override
Public void onAccuracyChanged (Sensor sensor, int accuracy ){
// TODO Auto-generated method stub

}
};

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
SensorManager = (SensorManager) getSystemService (SENSOR_SERVICE );
Sensor = sensorManager. getdefasensensor (Sensor. TYPE_ACCELEROMETER );
Vibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE );
Txt_content = (EditText) findViewById(R.id.txt _ content );
}

Private void showDialog (){
AlertDialog. Builder builder = new AlertDialog. Builder (this );
Dialog = builder. create ();
LayoutInflater inflater = LayoutInflater. from (this );
LinearLayout layout = (LinearLayout) inflater. inflate (
R. layout. alertdialog, null );
Dialog. setCanceledOnTouchOutside (false); // The blank area outside the click box does not make the dialog box disappear.
Dialog. show ();
Dialog. setContentView (layout, new LayoutParams (400,250 ));

Btn_delete = (Button) layout. findViewById (R. id. btn_delete );
Btn_delete.setOnClickListener (new OnClick ());
Btn_cancle = (Button) layout. findViewById (R. id. btn_cancle );
Btn_cancle.setOnClickListener (new OnClick ());
}

Class OnClick implements android. view. View. OnClickListener {

@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Switch (v. getId ()){
Case R. id. btn_delete:
Txt_content.getText (). clear ();
Dialog. dismiss ();
HasShaked = false;
Break;
Case R. id. btn_cancle:
Dialog. dismiss ();
HasShaked = false;
Default:
Break;
}
}

}

@ Override
Protected void onResume (){
// TODO Auto-generated method stub
Super. onResume ();
// Register a listener event
If (sensorManager! = Null ){
SensorManager. registerListener (listener, sensor,
SensorManager. SENSOR_DELAY_NORMAL );
}
}

@ Override
Protected void onPause (){
// TODO Auto-generated method stub
Super. onPause ();
// Cancel the listener
If (sensorManager! = Null ){
SensorManager. unregisterListener (listener );
}
}

}

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.