Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 自定義繪制android EditText的背景,定義EditText文字的顯示樣式

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

編輯:關於Android編程

EditText可以通過layer-list來繪制背景:

 




    
          //用白色來填充裡面
        
          //邊框的圓角的弧度
    


寫Layout:

 

 



代碼中實現一些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;
		}
	}

效果圖:

 

\

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved