目录:
    1.EditText输入框限制
        1.1 限制输入自定义字符串android:digits="" 

<EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="限制输入自定义的字符串"
            android:digits="1234567890+-*/=."
            android:singleLine="true"
             />

        


        1.2 限制输入数字和数字类型android:numeric=""(已弃用,由inputType代替)



<EditText
       android:id="@+id/editText2"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="限制输入数字"
       android:numeric="signed"
       android:singleLine="true"
       />

           


        1.3 限制输入电话号码android:phoneNumber=""(已弃用,由inputType代替)



<EditText
       android:id="@+id/editText3"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="限制输入电话号码"
       android:phoneNumber="true"
       android:singleLine="true"
       />

           


        1.4 限制是否可编辑android:enabled=""



<EditText
       android:id="@+id/editText4"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="限制不可编辑"
       android:enabled="false"
       android:singleLine="true"
       />

           


        1.5 限制输入密文android:password=""



<EditText
       android:id="@+id/editText5"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="限制输入密码"
       android:password="true"
       android:singleLine="true"
       />

    


        


        1.6 限制文本类型,选择软键盘android:inputType=""


                android:inputType=”number” 数字


                android:inputType=”numberSigned” 带符号数字格式


                android:inputType=”numberDecimal” 带小数点的浮点格式


                android:inputType=”phone” 拨号键盘


                android:inputType=”datetime” 时间日期


                android:inputType=”date” 日期键盘


                android:inputType=”time” 时间键盘            


                android:inputType=”none”


                android:inputType=”text”


                android:inputType=”textCapCharacters” 字母大写


                android:inputType=”textCapWords” 首字母大写


                android:inputType=”textCapSentences” 仅第一个字母大写


                android:inputType=”textAutoCorrect” 自动完成


                android:inputType=”textAutoComplete” 自动完成


                android:inputType=”textMultiLine” 多行输入


                android:inputType=”textImeMultiLine” 输入法多行(如果支持)


                android:inputType=”textNoSuggestions” 不提示


                android:inputType=”textUri” 网址


                android:inputType=”textEmailAddress” 电子邮件地址


                android:inputType=”textEmailSubject” 邮件主题


                android:inputType=”textShortMessage” 短讯


                android:inputType=”textLongMessage” 长信息


                android:inputType=”textPersonName” 人名


                android:inputType=”textPostalAddress” 地址


                android:inputType=”textPassword” 密码


                android:inputType=”textVisiblePassword” 可见密码


                android:inputType=”textWebEditText” 作为网页表单的文本


                android:inputType=”textFilter” 文本筛选过滤


                android:inputType=”textPhonetic” 拼音输入




                


        1.7 限制可输入文本长度


            android:maxLength=""(限制EditText最多显示多少个字符数。汉字、英文、数字都算一个字符。maxLength属性和ellipsize=”end”属性不共用)


<EditText
	        android:id="@+id/editText8"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:hint="maxLength設置字符數10"
	        android:maxLength="10"
	        android:singleLine="true"
	        />

            


            android:ems=""(设置EditText长度,字符长度,多出部分不显示,同时可使用ellipsize=”end”属性)


<EditText
	        android:id="@+id/editText6"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:ems="10"
	        android:hint="ems設置長度10"
	        android:singleLine="true"
	        /> <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">     </span>

    


    2.EditText其他常用属性


        2.1 设置背景颜色(该属性可以更换EditText的展示背景图片)


            1)xml实现:


<EditText 
    android:id="@+id/edit1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="设置背景颜色"
    android:background="#ff00ff"
    />



            2)代码实现:


EditText edit1 = (EditText)findViewById(.edit1);
edit1.setBackgroundColor(Color.GRAY);

    


        2.2 光标位置设置


            1) 应用场景:当EditView中包含文本是,需要将光标显示到文本末尾(默认在文本前面)方便用户对文本进行删除或增添操作,增强用户体验


            2) 实现代码:



EditText edit1 = (EditText)findViewById(.edit1);
edit1.setText("设置光标位置");		
edit1.setSelection(edit1.getText().length());

                


        2.3 焦点+软键盘弹出与隐藏(ps:焦点获取和键盘的自动弹出和隐藏并没有因果关系)


            1)焦点获取


EditText edit2 = (EditText)findViewById(.edit2);
edit2.setFocusable(true);
edit2.requestFocus();



            注:如果设置获取焦点,在页面进入时光标会显示在EditText上


            


            2)焦点取消


edit1 = (EditText)findViewById(.edit1);	
edit1.setFocusable(false);



            注:如果设置取消焦点,在页面进入时光标不会显示在EditText上,如此设置后EditText无法编辑


            


            3)在进入某个包含EditText的页面时,设置自动弹出/隐藏软键盘


                (1)通过Manifest.xml文件在activity主键注册时候设置windowSoftInputMode属性


<activity
    android:name=".SecondActivity"
    android:label="@string/app_name" 
    android:windowSoftInputMode="stateUnchanged|adjustPan">
</activity>

       


                (2)不设置windowSoftInputMode属性,将EditText(设置为获得焦点)放入ScrollView中,系统会认为用户需要输入,会默认显示软键盘


a.java代码



EditText edit2 = (EditText)findViewById(.edit2);
edit2.setFocusable(true); 
edit2.requestFocus();



b.xml布局文件


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http:///apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
	<ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">


        <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">


			<EditText 
			    android:id="@+id/edit1"
			    android:layout_width="match_parent"
			    android:layout_height="wrap_content"
			    android:hint="设置光标位置"
			    />  
			<EditText 
			    android:id="@+id/edit2"
			    android:layout_width="match_parent"
			    android:layout_height="wrap_content"
			    android:hint="设置背景颜色"
			    /> 
        </LinearLayout>


    </ScrollView>    
</LinearLayout>

            


            4)android:windowSoftInputMode=""属性介绍   


              (1)设置软键盘状态(显示隐藏属性)


                    stateVisible————————————>状态为可见状态(即使界面没输入框)


                    stateAlwaysVisible————————————>同样可以设置状态为可见,与stateVisible不同在于,你点击跳转下一页(软键盘不可见)再返回时软键盘依然可见


                    stateUnspecified————————————>状态未指定,由系统选择合适的方式,默认设置为它                                   


                    stateUnchanged————————————>指示状态不改变,延续上一个界面软键盘的状态


                    stateHidden————————————>状态为隐藏


                    stateAlwaysHidden————————————>状态为隐藏(暂时不清楚与stateHidden的区别)


                    


              (2)活动主窗口调整(设置活动窗口是否减小去适应软键盘显示/当软键盘覆盖内容时当前内容的焦点是否可见)


                    adjustUnspecified————————————>系统默认调整,当布局含有可滑动控件时,系统会调整显示大小适应键盘显示,可以滑动看见全部内容  如果不包含可滑动控件,软键盘可能会覆盖一部分内容,将获取焦点内容显示到中间,获取焦点的EditText靠下 部时,顶部的内容会被顶出不可见   


                                            


                    adjustPan————————————>不调整显示内容大小,没有滑动控件时,和默认效果一致,有滑动控件且获取焦点的EditText靠下部时, 顶部的内容会被顶出不可见


                    


                    adjustResize————————————>总是调整界面显示大小,有滑动控件时,可滑动查看内容,没有滑动控件时有部分内容会被遮挡住,但会挤压内容,如果 获取焦点的EditText靠下部时,顶部不会顶出,且无法看见获取焦点的EditText


                    


                    adjustNothing————————————>


        


        2.4 实现HTML的Textarea android:layout_height="200dp"


        


        2.5 设置字间距android:textScaleX="1.5"(设置倍数)           


        2.6 提示文字


            1)设置提示文字内容android:hint="提示文字"


            2)设置提示文字颜色android:textColorHint="#00ff00"(可自定义颜色)


        2.7 大写字母


            1)每个字母大写android:capitalize = "characters"/android:inputType=”textCapCharacters”


            2)每个单词首写大写android:capitalize = "words"/android:inputType=”textCapWords”


            3)仅仅第一个字母大写android:capitalize = "sentences"/android:inputType=”textCapSentences”




        2.8 设置最大/最小行(android:maxLines=""/android:minLines="")


        


        2.9 设置光标可见性:edit1.setCursorVisible(false/true);




        


    3.编辑框显示图片(设置以后图片无法删除)


        1)图片在文字上边:android:drawableTop="@drawable/ic_launcher"


        2)图片在文字下边:android:drawableBottom="@drawable/ic_launcher"


        3)图片在文字左边:android:drawableLeft="@drawable/ic_launcher"


        4)图片在文字右边:android:drawableRight="@drawable/ic_launcher"


        


    4.设置软键盘Enter键


        1)xml布局属性设置


   

android:imeOptions="normal"//正常显示,该输入框后面还有输入控件的时候会显示next,没有时会显示done(完成)


   android:imeOptions="actionUnspecified"//未指定则为键盘默认,与normal基本一致


   android:imeOptions="actionNone"//显示为回车键


   android:imeOptions="actionGo"//显示为Go(前往)按钮


   android:imeOptions="actionSearch"//显示搜索(Search)按钮


   android:imeOptions="actionSend"//显示send(发送)按钮


   android:imeOptions="actionNext"//显示next(下一步)按钮


   android:imeOptions="actionDone"//显示done(完成)按钮


   android:imeOptions="actionPrevious"//显示上一步按钮,如果前面有输入控件,点击后会回到前一个控件获取焦点


   android:imeOptions="flagNoFullscreen"//横屏时设置输入法不全屏


   android:imeOptions="flagNavigatePrevious"//横屏时输入法全屏,并在输入控件后面显示Enter键对应的操作


   android:imeOptions="flagNavigateNext"//横屏时输入法全屏,并在输入控件后面显示Enter键对应的操作,有返回前一个输入框的作用


   android:imeOptions="flagNoExtractUi"//横屏时设置输入法不全屏,与flagNoFullscreen的区别暂时不知道


   android:imeOptions="flagNoAccessoryAction"//横屏时输入法全屏,输入控件后面不显示Enter键对应的操作,键盘显示对应的操作键


   android:imeOptions="flagNoEnterAction"横屏时输入法不全屏,输入控件后面显示Enter键对应的操作,键盘不显示对应的操作键


                


        注意:在设置该属性时候需要和android:singleLine="true"或者android:inputType="text"任意一个属性一起才能生效


        


        2)java代码实现按钮监听(某些特定的按钮需要在点击后执行某些操作,所以需要我们去监听并执行相关操作)




        xml布局文件:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http:///apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
<ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">


       <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


<EditText 
   android:id="@+id/edit1"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:hint="none enter key"
   android:inputType="text"
   android:imeOptions="actionGo"
   />  
        </LinearLayout>


    </ScrollView>    


</LinearLayout>




        Java代码:


package com.andy.androiduiedittext;


import android.annotation.SuppressLint;
import .Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;


public class SecondActivity extends Activity {
private EditText edit1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
   edit1 = (EditText)findViewById(.edit1);
//设置edit1的key click监听
edit1.setOnEditorActionListener(new OnEditorActionListener() {

@SuppressLint("ShowToast")
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                //用switch可以在不确定actionId的情况下对不同的actionId进行不同的操作
switch (actionId) {
case EditorInfo.IME_ACTION_GO:
                    //toast提示信息
Toast.makeText(SecondActivity.this, "点击了go", Toast.LENGTH_SHORT).show();                    
break;


default:
break;
}
return false;
}

}

}

       


        3)设置Enter键的意义


            或许有伙伴会疑惑修改有啥作用,当然我举个例子,当我们在用百度或者谷歌搜索的时候,点击搜索框时键盘enter键就是search按钮


        以此方便用户搜索来增强用户体验,当然他的意义不止这一种,还有很多,再此记录方便自己及伙伴们遇到时查阅。


                                        


    5.监听软键盘的点击


        Java代码:


package com.andy.androiduiedittext;


import .Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;


public class SecondActivity extends Activity {
private EditText edit1;
private TextView text1;
private TextView text2;
private TextView text3;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.second_activity);
   edit1 = (EditText)findViewById(.edit1);
   text1 =(TextView)findViewById(.text1);
   text2 =(TextView)findViewById(.text2);
   text3 =(TextView)findViewById(.text3);
   edit1.addTextChangedListener(new TextWatcher() {
/*
* CharSequence:改变后的字符串
* start:改变后的字符串的起始位置
* before:输入框改变前字符串起始位置,默认为0
* count:改变字符串数量
*/
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
text1.setText("改变后的字符串:"+s.toString()+",start:"+start+",before:"+before+",count"+count);
}
/*
* CharSequence:改变前的字符串
* start:改变前字符串的起始位置
* count:输入框中改变前后的字符串改变数量
* after:输入框中改变后的字符串与起始位置的偏移量
*/ 
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
text2.setText("改变前的字符串:"+s.toString()+",start:"+start+",after:"+after+",count"+count);
}
/*
* Editable:改变后的字符串
*/ 
@Override
public void afterTextChanged(Editable s) {
text3.setText("改变后的字符串:"+s.toString());
}
});


super.onCreate(savedInstanceState);
}


}



        xml布局:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http:///apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


   <TextView 
   android:id="@+id/text1"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:hint="文本改变前"
   android:paddingBottom="10dp"
   /> 
<TextView 
   android:id="@+id/text2"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:hint="文本被改变"
   android:paddingBottom="10dp"
       /> 
<TextView 
   android:id="@+id/text3"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:hint="文本改变前后"
   />  


<EditText 
   android:id="@+id/edit1"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:hint="请输入文本"
   android:paddingBottom="10dp"
   android:text="aa"
   />   
</LinearLayout>

       


    6.明文与密文的切换(密码隐藏/显示)


        xml布局文件:


<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http:///apk/res/android"  
    android:id="@+id/textviewll"  
    android:orientation="vertical" 
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  


  
    <EditText    


      android:id="@+id/edit"      
      android:layout_width="fill_parent"     
      android:layout_height="wrap_content"   
      android:hint="请输入密码"
      android:inputType="textPassword"
      />  
      <CheckBox 
          android:id="@+id/checkbox"
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:text="显示密码"
          /> 


</LinearLayout>



 


        Java代码:


        


package com.andy.androiduiedittext;


import .Activity;  
import android.os.Bundle;  
import android.text.Editable;  
import android.text.Selection;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;  


public class SecondActivity extends Activity {  
     private EditText  edit1;
     private CheckBox  checkbox;
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_activity);  
    //绑定EditText
     edit1 = (EditText)findViewById(.edit);  
    //绑定CheckBox
     checkbox = (CheckBox)findViewById(.checkbox); 
     //设置监听
     checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//判断是否被选中,true显示,false隐藏
if (isChecked) { 
//设置InputType为可见密码
//1.setInputType实现显示密码
                //edit1.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                //2.setTransformationMethod实现显示密码
                //edit1.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                //3.通过setInputType设置系统分配的int码实现显示密码
                edit1.setInputType(0x009);
}else{
//设置为文本并设置隐藏密码
//1.setInputType实现隐藏密码,需要TYPE_CLASS_TEXT,TYPE_TEXT_VARIATION_PASSWORD二者同时使用才有效果
                //edit1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                //2.setTransformationMethod实现隐藏密码
                // edit1.setTransformationMethod(PasswordTransformationMethod.getInstance());                
                //3.通过setInputType设置系统分配的int码实现显示密码
                //TYPE_CLASS_TEXT 的值为 0x1 ,TYPE_TEXT_VARIATION_PASSWORD 的值为 0x80;0x081是二者的位与
edit1.setInputType(0x081);
}
            //设置光标在输入文本末尾,方便再输入
            Editable etable = edit1.getText();
            Selection.setSelection(etable, etable.length());
}
});  
    }
}

        


        


    7.EditText的全选、部分选择和获取选中文本


        xml布局文件:


<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http:///apk/res/android"  
    android:id="@+id/textviewll"  
    android:orientation="vertical" 
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  
  
<EditText    
    android:id="@+id/edit"      
    android:layout_width="fill_parent"     
    android:layout_height="wrap_content"   
    android:hint="请输入..."
    />  
<Button 
    android:id="@+id/allselect"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="全选"
    />
<Button 
    android:id="@+id/partselect"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="部分选择"
    />
<Button 
    android:id="@+id/getstring"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="获取选中的文本"
    />
<TextView 
    android:id="@+id/showstring"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_marginTop="20dp"
    android:textSize="20sp"
    android:text="获取文本内容"
    />
</LinearLayout>



        Java代码:


        


package com.andy.androiduiedittext;


import .Activity;  
import android.os.Bundle;  
import android.text.Editable;  
import android.text.Selection;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;  
import android.widget.TextView;
import android.widget.Toast;


public class SecondActivity extends Activity {  
     private EditText  edit1;
     private Button  allselectBt;
     private Button  partselectBt;
     private Button  getsringBt;
     private TextView  showstring;
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_activity);  
    //绑定控件
     edit1 = (EditText)findViewById(.edit); 
     allselectBt =(Button) findViewById(.allselect);
     partselectBt =(Button) findViewById(.partselect);
     getsringBt =(Button) findViewById(.getstring);
     showstring =(TextView) findViewById(.showstring);
     //设置全选edit文本内容
     allselectBt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
edit1.selectAll();
}
});
     //设置选择部分文本监听
     partselectBt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Editable editable = edit1.getText();
int length =editable.length();
if (length==0) {
Toast.makeText(SecondActivity.this, "文本内容为空", Toast.LENGTH_SHORT).show();

}else if(length>1){
//设置选择从第二个字符选择到字符结束
Selection.setSelection(editable, 1, length);
}else{
Toast.makeText(SecondActivity.this, "长度小于2", Toast.LENGTH_SHORT).show();
}
}

});
     //设置获取部分文本并显示的按钮监听
     getsringBt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
//获取edit文本
Editable editable = edit1.getText();
//获取文本长度
int length =editable.length();
if (length==0) {
Toast.makeText(SecondActivity.this, "文本内容为空", Toast.LENGTH_SHORT).show();
}else {
//获取光标的开始和结束
int start =edit1.getSelectionStart();
int end = edit1.getSelectionEnd();
//获取选择的文本
CharSequence  selectText =edit1.getText().subSequence(start, end);
//设置显示获取的文本
showstring.setText(selectText.toString());
}
}
});
  
    }
}

   


    8.EditText文本改变监听(参看5.监听软键盘的点击)


    


    9.EditText边框/下划线自定义


        注:EditText的边框和下划线由android:background=""属性决定,我们可以通过设置自己想要的背景来实现自定义EditText显示样式


      9.1)通过设置自定义图片(9.PNG格式)实现自定义背景:


        (1)如果需要设置不同状态的背景不同,需要准备几张图片导入drawable文件夹,然后在drawable文件夹下建edittext_imagebg_select.xml


        


    A)edittext_imagebg_select.xml


<selector xmlns:android="http:///apk/res/android">
    <item android:drawable="@drawable/image1.9.png" android:state_focused="false"/>
    <item android:drawable="@drawable/image2.9.png" android:state_focused="true"/>
</selector>

   


    B)activty_main.xml 


<LinearLayout xmlns:android="http:///apk/res/android"
    xmlns:tools="http:///tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.andy.androiduiedittext.MainActivity" >


    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="圖片背景"
        android:background="@drawable/back"
        android:singleLine="true"
         />


</LinearLayout>

 


        (2)如果不需要我们可以直接在控件布局中设置android:background=""属性


            


      9.2)通过Shape绘制实现自定义背景:


        (1)edittext_shapebg_normal.xml(正常)      


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http:///apk/res/android" >
    
    <!-- 设置圆角 -->
    <corners android:radius="5dp">
    </corners>
    <!-- 设置填充色为白色 -->
    <solid android:color="#ffffff"/>
    <!-- 设置描边颜色和宽度 -->
    <stroke android:color="#000000"
        android:width="1dp">
    </stroke>
</shape>

   


        (2)edittext_shapebg_focused.xml(获取焦点时)    


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http:///apk/res/android" >
    
    <!-- 设置圆角 -->
    <corners android:radius="5dp">
    </corners>
    <!-- 设置填充色为白色 -->
    <solid android:color="#ffffff"/>
    <!-- 设置描边颜色和宽度 -->
    <stroke android:color="#00BFFF"
        android:width="1dp">
    </stroke>
</shape>

     


        (3)edittext_shapebg_select.xml


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http:///apk/res/android" >
    <item android:state_focused="false" android:drawable="@drawable/edittext_shapebg_normal"></item>
<item android:state_focused="true" android:drawable="@drawable/edittext_shapebg_focused"></item>
</selector>



        (4)布局文件activity_main.xml     


<LinearLayout xmlns:android="http:///apk/res/android"
    xmlns:tools="http:///tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.andy.androiduiedittext.MainActivity" >
    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="shape绘制背景"
        android:padding="10dp"
        android:gravity="bottom|center_vertical"
        android:background="@drawable/edittext_shapebg_select"
        android:singleLine="true"
         />
</LinearLayout>



      9.3)shap绘制背景效果截图 

android edittext requestFocus 出现阴影_android

           


    10.制作9.PNG格式图片


    


        注:9.PNG图片会有拉伸图片不失真的效果,在android的世界里面会有很多分辨率,而在不同的分辨率下同一张图片显示效果可能就会不同,这样很影响用户的体验, 9.PNG可以很有效的保证图片在不同分辨率下的显示效果。


    


      10.1) 找到安装android-sdk的路径下的draw9patch.bat批处理文件点击打开


      10.2) 拖拽或点击File open导入需要处理的PNG图片

android edittext requestFocus 出现阴影_输入框_02

      10.3) 由图片可以看出draw9patch工具有两个区,左边是工作区,右边是拉伸图片预览区域,下边是一些放大缩小或者和相关选项,好熟悉工作区后,我需要在左边区域


    制作我们的9.png图片。


      10.4) 围绕我们图片的四周会有一个矩形,而我们图片外边缘会有1px的空白,我们需要在每一边的边缘空白处点击鼠标拖拽自定义长度的区域(如果多选可以按住ctrl键


      去掉不想选择的部分),当我们在4边都拖拽出自定义的线条后,我们的9.png图片就制作完成了,点击File保存即可。

android edittext requestFocus 出现阴影_android_03

      


      ps:图片被分成了9个区域,中心部分(5)为内容区域,横向中间3块(4,5,6)区域为可以上下拉伸的区域,纵向中间3个区域(2,5,8)为可以左右拉伸的区域,在制作的时


      候伙伴们可以根据实际情况自定义