国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android TextWatcher監(jiān)控EditText中的輸入內(nèi)容并限制其個(gè)數(shù)

2019-12-12 03:08:16
字體:
供稿:網(wǎng)友

布局中EditText在android布局中經(jīng)常用到,對(duì)EditText中輸入的內(nèi)容也經(jīng)常需要進(jìn)行限制,我們可以通過TextWatcher去觀察輸入框中輸入的內(nèi)容,作個(gè)筆記。

主布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  ><TextView android:id="@+id/tv"  android:layout_width="fill_parent"   android:layout_height="wrap_content"   android:textColor="@android:color/white"   android:ellipsize="marquee"   android:focusable="true"   android:marqueeRepeatLimit="marquee_forever"   android:focusableInTouchMode="true"   android:scrollHorizontally="true"    android:text="Please input the text:"  /><EditText android:id="@+id/ET"   android:layout_width="match_parent"   android:layout_height="wrap_content"  android:inputType="number"/></LinearLayout>

java代碼:

package com.android.text;import android.app.Activity;import android.os.Bundle;import android.text.Editable;import android.text.TextWatcher;import android.util.Log;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class TextWatcherDemo extends Activity {  private TextView mTextView;  private EditText mEditText;  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    mTextView = (TextView)findViewById(R.id.tv);    mEditText = (EditText)findViewById(R.id.ET);    mEditText.addTextChangedListener(mTextWatcher);  }  TextWatcher mTextWatcher = new TextWatcher() {    private CharSequence temp;    private int editStart ;    private int editEnd ;    @Override    public void beforeTextChanged(CharSequence s, int arg1, int arg2,        int arg3) {      temp = s;    }    @Override    public void onTextChanged(CharSequence s, int arg1, int arg2,        int arg3) {      mTextView.setText(s);    }    @Override    public void afterTextChanged(Editable s) {      editStart = mEditText.getSelectionStart();      editEnd = mEditText.getSelectionEnd();      if (temp.length() > 10) {        Toast.makeText(TextWatcherDemo.this,            "你輸入的字?jǐn)?shù)已經(jīng)超過了限制!", Toast.LENGTH_SHORT)            .show();        s.delete(editStart-1, editEnd);        int tempSelection = editStart;        mEditText.setText(s);        mEditText.setSelection(tempSelection);      }    }  };}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 满城县| 珠海市| 黄山市| 武平县| 乐昌市| 临猗县| 都匀市| 盐山县| 长泰县| 蕉岭县| 青神县| 柞水县| 玛曲县| 广宁县| 南郑县| 台北市| 合山市| 屏南县| 贺兰县| 绩溪县| 新竹县| 顺义区| 普安县| 澳门| 万山特区| 青浦区| 清水县| 襄垣县| 含山县| 天全县| 通州区| 西畴县| 西平县| 黄冈市| 平舆县| 刚察县| 建湖县| 沅江市| 类乌齐县| 公安县| 惠安县|