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

來源:互聯網
上載者:User

標籤:處理   分享   oid   otto   cte   ext.get   矩形   dip   horizon   

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;@Overrideprotected 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;@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {temp = s;}@Overridepublic 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文字的顯示樣式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.