当前位置: 移动技术网 > 移动技术>移动开发>Android > Android EditText详解及示例代码

Android EditText详解及示例代码

2019年07月24日  | 移动技术网移动技术  | 我要评论

    edittext在api中的结构

  java.lang.object

  android.view.view

  android.widget.textview

  android.widget.edittext

  已知直接子类:

  autocompletetextview, extractedittext

  已知间接子类:

  multiautocompletetextview

  edittext是textview的直接子类 所以edittext会继承父类textview的一些方法。下面我用自己写的一个demo 和大家详细的说明一下editview的使用方法。

  1.简单的edittext输入框

  非常简单,在layout布局中配置一下edittext 在配置一个button 在代码中监听button 的事件 获取当前editview中输入的内容并且显示出来。

xml/html代码

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/textviewll" 
  android:orientation="vertical" android:layout_width="fill_parent" 
  android:layout_height="fill_parent"> 
  <edittext 
   android:id="@+id/sample_edit_text0" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="简单的edittext输入框"/> 
  <button 
   android:id="@+id/sample_button0" 
   android:layout_width="fill_parent" android:layout_height="wrap_content" 
   android:text="确定"/> 
</linearlayout> 

java代码

public class sampleactivity extends activity { 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
  setcontentview(r.layout.sample); 
  
  final edittext edittext0 = (edittext)findviewbyid(r.id.sample_edit_text0); 
  
  button button0 = (button)findviewbyid(r.id.sample_button0); 
  
  button0.setonclicklistener(new onclicklistener() { 
  
    @override 
    public void onclick(view arg0) { 
    string str = edittext0.gettext().tostring(); 
    toast.maketext(sampleactivity.this,str, toast.length_long).show(); 
    } 
  }); 
  
  super.oncreate(savedinstancestate); 
  } 
} 

  2.限制edittext输入框的内容

  在layout中配置信息

  android:digits=”1234567890.+-*/%\n()”

  限制输入框中只能输入自己定义的这些字符串 如果输入其它将不予以显示

  android:phonenumber=”true”

  限制输入框中只能输入手机号码

  android:password=”true”

  限制输入框中输入的任何内容将以”*”符号来显示

  android:hint=”默认文字”

  输入内容前默认显示在输入框中的文字

  android:textcolorhint=”#ff0000″

  设置文字内容颜色

  android:enabled=”false”

  设置输入框不能被编辑

  3.编辑框中显示图片

  上一篇讲textview中就讲过在textview中添加图片的方法,因为edittext是textview的子类, 所以当然也可以添加图片了,只是一旦在edittext中添加图片以后是不能删除的,如图所示我可以编辑图片旁边的内容,写入文字。

xml/html代码

<?xml version="1.0" encoding="utf-8"?> 
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/textviewll" 
  android:orientation="vertical" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  > 
  <edittext 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="在图片下方" 
       android:textcolor="#ff0000" 
       android:drawablebottom="@drawable/jay" 
       android:layout_alignparenttop="true" 
       android:layout_centerhorizontal="true" 
       > 
   </edittext> 
  
   <edittext 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="在图片上方" 
       android:textcolor="#ff0000" 
       android:drawabletop="@drawable/jay" 
       android:layout_alignparentbottom="true" 
       android:layout_centerhorizontal="true" 
       > 
   </edittext> 
   <edittext 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="在图片左边" 
       android:textcolor="#ff0000" 
       android:drawableleft="@drawable/jay" 
       android:layout_alignparentleft="true" 
       android:layout_centervertical="true" 
       > 
   </edittext> 
   <edittext 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="在图片右边" 
       android:textcolor="#ff0000" 
       android:drawableright="@drawable/jay" 
       android:layout_alignparentright="true" 
       android:layout_centervertical="true" 
       > 
   </edittext>  
  
</relativelayout > 

  4.设置软键盘的enter键

  如图所示我们可以修改软键盘的enter按钮的样式,可以在代码中监听 按钮点击事件。

java代码

package cn.m15.xys; 
  
import android.app.activity; 
import android.os.bundle; 
import android.view.keyevent; 
import android.view.inputmethod.editorinfo; 
import android.widget.edittext; 
import android.widget.textview; 
import android.widget.toast; 
import android.widget.textview.oneditoractionlistener; 
  
public class keyboardactivity extends activity { 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
  setcontentview(r.layout.keyboard); 
  
  edittext edittext0 = (edittext)findviewbyid(r.id.txttest0); 
  
  edittext0.setoneditoractionlistener(new oneditoractionlistener() { 
  
    @override 
    public boolean oneditoraction(textview arg0, int arg1, keyevent arg2) { 
    if (arg1 == editorinfo.ime_action_go) { 
      toast.maketext(keyboardactivity.this, "你点了软键盘'去往'按钮", 
        toast.length_short).show(); 
    } 
    return false; 
    } 
  }); 
  edittext edittext1 = (edittext)findviewbyid(r.id.txttest1); 
  
  edittext1.setoneditoractionlistener(new oneditoractionlistener() { 
  
    @override 
    public boolean oneditoraction(textview arg0, int arg1, keyevent arg2) { 
    if (arg1 == editorinfo.ime_action_search) { 
      toast.maketext(keyboardactivity.this, "你点了软键盘'搜索'按钮", 
        toast.length_short).show(); 
    } 
    return false; 
    } 
  }); 
  edittext edittext2 = (edittext)findviewbyid(r.id.txttest2); 
  
  edittext2.setoneditoractionlistener(new oneditoractionlistener() { 
  
    @override 
    public boolean oneditoraction(textview arg0, int arg1, keyevent arg2) { 
    if (arg1 == editorinfo.ime_action_send) { 
      toast.maketext(keyboardactivity.this, "你点了软键盘'发送'按钮", 
        toast.length_short).show(); 
    } 
    return false; 
    } 
  }); 
  edittext edittext3 = (edittext)findviewbyid(r.id.txttest3); 
  
  edittext3.setoneditoractionlistener(new oneditoractionlistener() { 
  
    @override 
    public boolean oneditoraction(textview arg0, int arg1, keyevent arg2) { 
    if (arg1 == editorinfo.ime_action_next) { 
      toast.maketext(keyboardactivity.this, "你点了软键盘'下一个'按钮", 
        toast.length_short).show(); 
    } 
    return false; 
    } 
  }); 
  edittext edittext4 = (edittext)findviewbyid(r.id.txttest4); 
  
  edittext4.setoneditoractionlistener(new oneditoractionlistener() { 
  
    @override 
    public boolean oneditoraction(textview arg0, int arg1, keyevent arg2) { 
    if (arg1 == editorinfo.ime_action_done) { 
      toast.maketext(keyboardactivity.this, "你点了软键盘'完成'按钮", 
        toast.length_short).show(); 
    } 
    return false; 
    } 
  }); 
  edittext edittext5 = (edittext)findviewbyid(r.id.txttest5); 
  
  edittext5.setoneditoractionlistener(new oneditoractionlistener() { 
  
    @override 
    public boolean oneditoraction(textview arg0, int arg1, keyevent arg2) { 
    if (arg1 == editorinfo.ime_action_unspecified) { 
      toast.maketext(keyboardactivity.this, "你点了软键盘'未指定'按钮", 
        toast.length_short).show(); 
    } 
    return false; 
    } 
  }); 
  super.oncreate(savedinstancestate); 
  } 
} 

  监听软键盘的点击事件

xml/html代码

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/textviewll" 
  android:orientation="vertical" android:layout_width="fill_parent" 
  android:layout_height="fill_parent"> 
  <edittext android:id="@+id/txttest0" 
   android:imeoptions="actiongo" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:hint="特殊按钮-去往" 
   ></edittext> 
  <edittext android:id="@+id/txttest1" 
   android:imeoptions="actionsearch" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:hint="特殊按钮-搜索" 
   ></edittext> 
  <edittext android:id="@+id/txttest2" 
   android:imeoptions="actionsend" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:hint="特殊按钮-发送" 
   ></edittext> 
  <edittext android:id="@+id/txttest3" 
   android:imeoptions="actionnext" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:hint="特殊按钮-下一个" 
   ></edittext> 
  <edittext android:id="@+id/txttest4" 
   android:imeoptions="actiondone" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:hint="特殊按钮-完成" 
   ></edittext> 
  <edittext android:id="@+id/txttest5" 
   android:imeoptions="actionunspecified" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:hint="特殊按钮-未指定" 
   ></edittext> 
  
</linearlayout> 

  5.监听软键盘的按键事件

  做项目的时候 有时候须要在用户输入内容时做检测,比如如果用户输入不合法的内容不予以显示在edittext中, 这时候我就要用到addtextchangedlistener 用它来监听用户输入状态。可以在监听中改变用户输入的内容或者提示用户输入内容不合法等等。 如图所示我的每次输入操作都可以被正常的监听出来,用户输入内容的正常流程 beforetextchanged()  -》ontextchanged()  -》aftertextchanged()然后是通知屏幕绘制 显示在屏幕上 所以我们可以在这三个方法中来修改用户输入内容 或者截取用户输入的内容。

java代码

package cn.m15.xys; 
  
import android.app.activity; 
import android.os.bundle; 
import android.text.editable; 
import android.text.textwatcher; 
import android.widget.edittext; 
import android.widget.textview; 
  
public class monitorkeyactivity extends activity { 
  @override 
  protected void oncreate(bundle savedinstancestate) { 
  setcontentview(r.layout.monitorkey); 
  edittext edittext = (edittext)findviewbyid(r.id.monitor_edit_text0); 
  final textview textview0 = (textview)findviewbyid(r.id.monitor_text0); 
  final textview textview1 = (textview)findviewbyid(r.id.monitor_text1); 
  final textview textview2 = (textview)findviewbyid(r.id.monitor_text2); 
  
  edittext.addtextchangedlistener(new textwatcher() { 
  
    @override 
    public void ontextchanged(charsequence text, int start, int before, int count) { 
        //text 输入框中改变后的字符串信息 
    //start 输入框中改变后的字符串的起始位置 
    //before 输入框中改变前的字符串的位置 默认为0 
    //count 输入框中改变后的一共输入字符串的数量 
    textview1.settext("输入后字符串 [ " + text.tostring() + " ] 起始光标 [ " + start + " ] 输入数量 [ " + count+" ]"); 
  
    } 
  
    @override 
    public void beforetextchanged(charsequence text, int start, int count,int after) { 
    //text 输入框中改变前的字符串信息 
    //start 输入框中改变前的字符串的起始位置 
    //count 输入框中改变前后的字符串改变数量一般为0 
    //after 输入框中改变后的字符串与起始位置的偏移量 
    system.out.println(text.tostring()); 
    textview0.settext("输入前字符串 [ " + text.tostring() + " ]起始光标 [ " + start + " ]结束偏移量 [" + after + " ]"); 
    } 
  
    @override 
    public void aftertextchanged(editable edit) { 
    //edit 输入结束呈现在输入框中的信息 
    textview2.settext("输入结束后的内容为 [" + edit.tostring()+" ] 即将显示在屏幕上"); 
    } 
  }); 
  
  super.oncreate(savedinstancestate); 
  } 
} 

xml/html代码

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:id="@+id/textviewll" 
  android:orientation="vertical" android:layout_width="fill_parent" 
  android:layout_height="fill_parent"> 
  <textview 
   android:id="@+id/monitor_text0" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:textsize="18dip" 
   android:textcolor="#ff0000"/> 
  <textview 
   android:id="@+id/monitor_text1" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:textsize="18dip" 
   android:textcolor="#ff0000" 
   /> 
  <textview 
   android:id="@+id/monitor_text2" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:textsize="18dip" 
   android:textcolor="#ff0000" 
   /> 
  <edittext 
   android:id="@+id/monitor_edit_text0" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:hint="监听软键盘按键的输入状态"/> 
</linearlayout> 

        希望通过此文,大家可以对android rdittext的知识掌握,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网