Android Simple Combat Tutorial--the 19th session of "Teach you to monitor edittext text changes, to achieve the effect of jitter and vibration"

Source: Internet
Author: User

Listen to the text input situation, just limited to the toast slightly low point, this article is slightly "tall" some, experience rolling and vibration.

First, two files are required. :

The contents of the two files are as follows:

Cycle_7:

<?xml version= "1.0" encoding= "Utf-8"?><!--indicates the number of cycles--><cycleinterpolator xmlns:android= "/http/ Schemas.android.com/apk/res/android "    android:cycles=" 7 "/>

Shake.xml:

<?xml version= "1.0" encoding= "Utf-8"?><!--displacement animation. Time 1 seconds, displacement X (0,10). Jitter Cycle times 7 times. Interpolator is the animated insert--><translate xmlns:android= "http://schemas.android.com/apk/res/android"    android: duration= "    android:fromxdelta=" 0 "    android:interpolator=" @anim/cycle_7 "    android:toxdelta=" 10 "/ >
And then we go to the subject. Let's start with an interface:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "vertical"    tools:context= ". Mainactivity ">    <edittext        android:id=" @+id/et_input "        android:layout_width=" Match_parent        " android:layout_height= "Wrap_content"        android:hint= "Please enter the password"/>    <button        android:id= "@+id/bt_ Query "        android:layout_width=" wrap_content "        android:layout_height=" wrap_content "        android:text=" Query "/>    <textview        android:id=" @+id/tv_showresult "        android:layout_width=        " Match_parent " android:layout_height= "Wrap_content"/></linearlayout>

Requirements: Enter the password in the edit box to determine if it is correct. If the input is correct, it will be displayed correctly on the bottom (listen text changes here); If the error, the phone vibration (vibration effect requires permission, real machine test), if the input is empty, edit box to scroll left and right.

The code writes out:

Package Com.itydl.shake;import Android.app.activity;import Android.os.bundle;import android.os.vibrator;import Android.text.editable;import Android.text.textutils;import Android.text.textwatcher;import Android.view.View; Import Android.view.view.onclicklistener;import Android.view.animation.animation;import Android.view.animation.animationutils;import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;public class Mainactivity extends Activity {private EditText et_input;private TextView Tv_resu Lt;private Button bt_query; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (Savedin        Stancestate);                Setcontentview (R.layout.activity_main);                Et_input = (EditText) Findviewbyid (r.id.et_input);                Tv_result = (TextView) Findviewbyid (R.id.tv_showresult);                Bt_query = (Button) Findviewbyid (r.id.bt_query);    Initevent (); }private void Initevent () {//listen for text changes. EditText text ChangesCall the appropriate method below Et_input.addtextchangedlistener (new Textwatcher () {@Overridepublic void ontextchanged (Charsequence s, int start, int before, int count) {///text changed when called} @Overridepublic void beforetextchanged (charsequence s, int start, int coun T,int after) {///text changed before calling} @Overridepublic void aftertextchanged (Editable s) {//text changed after call Showresult ();});} public void Showresult () {Bt_query.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {// Gets the edit box contents of String inputpass = Et_input.gettext (). toString (). Trim ();//Determines whether the input is empty, or null to implement the scrolling effect if (Textutils.isempty (inputpass ) {//is empty, scrolling animation shake = Animationutils.loadanimation (Mainactivity.this, r.anim.shake);//requires R.anim.shake resource file et_ Input.startanimation (shake); Tv_result.settext (""); return;} Determine if the mask is 123if (Inputpass.equals ("123")) {Tv_result.settext ("Congratulations, right!") ");} else{//not correct, let the phone vibrate */* Vibration effect */vibrator vibrator = (vibrator) getsystemservice (vibrator_service);//Vibration parameter setting (two parameters: 1, vibration, rest, Vibration, rest .... Alternating cycles, 2, repetitions of 3 times) vibrator.vibrate (new long[]{200,300,300,200,500,100}, 3);                           Tv_result.settext ("");     }});} }

Vibration effect Remember to add the power of vibration: (Vibration of the real machine to test OH)

<uses-permission android:name= "Android.permission.VIBRATE"/>

Well, run the program to see the effect:

What do you think? than toast, is not slightly "tall on" it? Let's play!

Welcome to my Blog: Click to open the link to open the link http://blog.csdn.net/qq_32059827 watch more and better play the case, daily a more Oh!

Android Simple Combat Tutorial--the 19th session of "Teach you to monitor edittext text changes, to achieve the effect of jitter and vibration"

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.