Android簡易實戰教程--第十九話《手把手教您監聽EditText文本變化,實現抖動和震動的效果》

來源:互聯網
上載者:User

標籤:

監聽文本輸入情況,僅僅限於土司略顯low點,這一篇就稍微“高大上”些,體驗一下滾動和震動。

首先,需要兩個檔案。:

兩個檔案的內容分別如下:

cycle_7:

<?xml version="1.0" encoding="utf-8"?><!-- 表示迴圈的次數 --><cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"    android:cycles="7" />

shake.xml:

<?xml version="1.0" encoding="utf-8"?><!-- 位移動畫 。時間1秒,位移x(0,10)。抖動迴圈次數7次。interpolator是動畫插入器--><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="1000"    android:fromXDelta="0"    android:interpolator="@anim/cycle_7"    android:toXDelta="10" />
接下來進入主題了。先來個介面:

<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="請輸入密碼" />    <Button        android:id="@+id/bt_query"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="查詢" />    <TextView        android:id="@+id/tv_showresult"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>

需求:在編輯框輸入密碼,判斷是否正確。如果輸入正確,就在下邊顯示正確(這裡做監聽文本變化);如果錯誤,手機震動(震動效果需要許可權、真機測試);如果輸入為空白,編輯框實現左右滾動。

代碼寫了出來:

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_result;private Button bt_query;@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        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() {//監聽文本變化.EditText文本發生變的時候調用下面相應的方法et_input.addTextChangedListener(new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {// 文本變化的時候調用}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {// 文本變化之前調用}@Overridepublic void afterTextChanged(Editable s) {// 文本變化之後調用showResult();}});}public void showResult(){bt_query.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//擷取編輯框內容String inputpass = et_input.getText().toString().trim();//判斷輸入是否為空白,為空白就實現滾動效果if(TextUtils.isEmpty(inputpass)){//為空白,滾動Animation shake = AnimationUtils.loadAnimation(MainActivity.this, R.anim.shake);//需要R.anim.shake資源檔et_input.startAnimation(shake);tv_result.setText("");return;}//判斷面膜是否是123if(inputpass.equals("123")){tv_result.setText("恭喜你,正確!");}else{//不正確,讓手機震動/* * 震動效果 */Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);//震動的參數設定   (兩個參數:1、震動、休息、震動、休息....交替迴圈;2、重複次數為3次)   vibrator.vibrate(new long[]{200,300,300,200,500,100}, 3);                                        tv_result.setText("");                            }}});}    }

震動效果記得加上震動的許可權:(震動真機才能測試哦)

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

好了,運行程式看看效果:

怎麼樣?比起土司,是不是略顯“高大上”呢?趕快玩起來吧!

歡迎大家關注我的部落格:點擊開啟連結      點擊開啟連結   http://blog.csdn.net/qq_32059827      觀看更多更好玩的案例,每日一更哦!

Android簡易實戰教程--第十九話《手把手教您監聽EditText文本變化,實現抖動和震動的效果》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.