1. 程式人生 > >自己定義繪制android EditText的背景,定義EditText文字的顯示樣式

自己定義繪制android EditText的背景,定義EditText文字的顯示樣式

ffffff utf-8 message fff 邏輯 inpu final tro adding

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文字的顯示樣式