亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

從零開發Android系列(2)-控件篇-EditText

EditText是一个输入框,用户可以往里面输入文字,我们这边做拿EditText做一个登录界面

https://img1.sycdn.imooc.com//5c07227100016dcd04320726.jpg

我们来写一下xml文件,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/desktopnoticebg">
   <ImageView 
       android:id="@+id/login_titlebar"
      android:layout_height="66dp" 
      android:layout_width="fill_parent"
      android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@drawable/kaixinwang_login"
      android:scaleType="centerInside"
      android:background="@drawable/titlebg1pix_login">  
   </ImageView>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/login_idlayout"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="25dp"
       android:layout_below="@id/login_titlebar"
       android:layout_centerHorizontal="true">
      <TextView
          android:id="@+id/login_tvID"  
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="@string/login_id"
          android:layout_marginTop="4dp"
          android:textColor="@drawable/black"
          android:textSize="20sp"
          />
      <EditText 
          android:id="@+id/login_etID"
          android:layout_width="220dp"
          android:layout_height="wrap_content" 
          android:layout_toRightOf="@id/login_tvID"
          android:layout_marginLeft="4dp"
          android:singleLine="true"
          android:inputType="textEmailAddress"
          android:maxLength="300"
          />
   </RelativeLayout>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/login_pwdlayout"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginTop="10dp"
       android:layout_below="@id/login_idlayout"
       android:layout_centerHorizontal="true">
      <TextView
          android:id="@+id/login_tvPwd"  
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="@string/login_pwd"
          android:layout_marginTop="4dp"
          android:textColor="@drawable/black"
          android:textSize="20sp"
          />
      <EditText 
          android:id="@+id/login_etPwd"
          android:layout_width="220dp"
          android:layout_height="wrap_content"
          android:inputType="textPassword"
          android:singleLine="true"
          android:layout_marginLeft="4dp"
          android:layout_toRightOf="@id/login_tvPwd"
          android:maxLength="300"
          />
      <Button 
          android:id="@+id/login_btnLogin"
          android:layout_width="220dp"
          android:layout_height="wrap_content"
          android:text="@string/login_btn"
          android:layout_below="@id/login_etPwd"
          android:layout_alignLeft="@id/login_etPwd"
          android:textSize="20sp"
          android:layout_marginTop="10dp"
          />
   </RelativeLayout>
</RelativeLayout>

我们看到EditText有一个很重要的属性是inputType,可以指定输入是密码,还是邮箱等等

如何去监听用户的输入呢,android提供了TextWatch接口,

m_txtPassword.addTextChangedListener(new TextWatcher() {

   @Override
   public void onTextChanged(CharSequence s, int start, int before,  int count) {}

   @Override
   public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

   @Override
   public void afterTextChanged(Editable s) {}
});

其中onTextChanged的参数意思是:

其中有4个参数: 
CharSequence s:文本改变之后的内容 
int start : 被替换文本区域起点位置,setText时是替换所有内容,此时数值为0 
int before:被替换之前的文本区域字符数目 
int count:替换后的文本字符数目

afterTextChanged的参数意思是:

Editable s:文本改变之后的内容


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消