自定义绘制android EditText的背景,定义EditText文字的显示样式

EditText可以通过layer-list来绘制背景:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle" //框为矩形
        >
        <solid android:color="#FFFFFF" />  //用白色来填充里面
        <stroke //边框是1dp的线
            android:width="1dp"
            android:color="#AEAEAE"
            />
        <corners android:radius="10dp"/>  //边框的圆角的弧度
    </shape>
</item>
</layer-list>

写Layout:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#FFFFFF"
     >

    <include 
        layout="@layout/title_list"
        />

    <View
        android:layout_below="@id/title"
        android:id="@+id/line"
        android:layout_width="fill_parent"
        android:layout_height="2dip"
        android:background="@drawable/rc_list_divider" />
    
     <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="150dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_centerInParent="true">

        <EditText
            android:id="@+id/custom_edittext"
            android:layout_width="fill_parent" 
            android:layout_height="150dp"  //跟parent height一致所以left_word_num可以在背景框里面
            android:background="@drawable/reply_custom_message_edit_background"
            android:gravity="left|top"
            android:hint="please inputsomething"
            android:paddingBottom="24dp"
            android:paddingLeft="12dp"
            android:paddingRight="12dp"
            android:paddingTop="17dp"
            android:textColor="#000000"
            android:textColorHint="#AEAEAE"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/other_char_hint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/custom_edittext"
            android:layout_alignParentBottom="true"
            android:paddingBottom="10dp"
            android:paddingLeft="12dp"
            android:text="0"
            android:textColor="#AEAEAE"
            android:textSize="14sp"
            android:visibility="gone"></TextView>

        <TextView
            android:id="@+id/left_word_num"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:paddingBottom="10dp"
            android:paddingRight="10dp"
            android:text="200"
            android:textColor="#000000"
            android:textSize="14sp"></TextView>
    </RelativeLayout>
    
</RelativeLayout>

代码中实现一些EditText变化的时候处理逻辑(目前的逻辑是不让输入中文字符:

private EditText mCustom_edittext;
	private TextView left_word_num;
	private static final int MAX_INPUT_NUM = 200;
	private TextView other_char_hint;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.edittext);
		mCustom_edittext = (EditText)findViewById(R.id.custom_edittext);
		other_char_hint = (TextView)findViewById(R.id.other_char_hint);
		other_char_hint.setText("only enlish accepted");
		left_word_num = (TextView) findViewById(R.id.left_word_num);
		mCustom_edittext.addTextChangedListener(new TextWatcher() {

			private CharSequence temp;
			private int selectionStart;
			private int selectionEnd;

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

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

			@Override
			public void afterTextChanged(Editable s) {
				selectionStart = mCustom_edittext.getSelectionStart();
				selectionEnd = mCustom_edittext.getSelectionEnd();
				
				String lastword = s.toString().trim();
				if (lastword.length() > 0) {
					lastword = lastword.substring(lastword.length() - 1, lastword.length());
					if(!isContentValid(temp.toString())) {
						other_char_hint.setVisibility(View.VISIBLE);
						for(int i=0;i < temp.toString().length();i++) {
							String temp1 = temp.toString().substring(i, i+1);
							if(!isInputValid(temp1)) {
								//使用setSpan使EditText字底下画黑线 
								s.setSpan(new UnderlineSpan(), i, i+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
							}
						}
					}else{
						other_char_hint.setVisibility(View.GONE);
					}
					
				}else{
					other_char_hint.setVisibility(View.GONE);
				}
				left_word_num.setText(String.valueOf(MAX_INPUT_NUM - temp.length()));
				
			}

		});
		
	}
	
	private boolean isContentValid(String content) {
		for(int i=0;i < content.length(); i++) {
			String value = content.substring(i,i+1);
			if(!isInputValid(value)) {
				return false;
			}
		}
		return true;
	}

	private boolean isInputValid(String s) {
		byte[] ch_array = s.getBytes();
		if (ch_array.length == 1) {
			return true;
		} else {
			return false;
		}
	}

效果图:

自定义绘制android EditText的背景,定义EditText文字的显示样式

自定义绘制android EditText的背景,定义EditText文字的显示样式,布布扣,bubuko.com

自定义绘制android EditText的背景,定义EditText文字的显示样式

上一篇:Android Rom移植


下一篇:Android 性能优化 四 布局优化merge标签的使用