自己定义绘制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;
		}
	}

效果图:

时间: 2024-08-15 14:51:32

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

自定义绘制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" //框为矩形 > &l

Android开发之自己定义TabHost文字及背景(源码分享)

使用TabHost 能够在一个屏幕间进行不同版面的切换,而系统自带的tabhost界面较为朴素,我们应该怎样进行自己定义改动优化呢 MainActivity的源码 package com.dream.ledong; import android.app.TabActivity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.Gr

Android应用之——自己定义控件ToggleButton

我们经常会看到非常多优秀的app上面都有一些非常美丽的控件,用户体验非常好.比方togglebutton就是一个非常好的样例,IOS系统以下那个精致的togglebutton现在在android以下也能够实现了,并且还能够自己定义它的颜色文字背景图,做出各种美丽的开关按键出来. 这里就用到了android里面一个比較经常使用的技术--自己定义控件. 先来看下我们实现的自己定义的togglebutton效果图:      自己定义控件的步骤: 1.首先,定义一个类继承View 或者View的子类,

Android中如何自己定义吐司(Toast)

Android系统里面有个东西很好用,也很常用,那就是Toast,但是长期使用也会发现,Toast有他的不足之处:形式单一,只有文字,风格不变等等,那么要如何自定义一个Toast呢,我们可以先从分析Android定义Toast的代码着手: Toast的makeText方法: 这里实际上Android所做的工作是将Toast显示的文本和持续时间设置了一下,然后返回了Toast对象,用以执行show()方法.这里核心的地方是要弄明白 这一句Android做了一些什么工作,下面我们继续看: 这里实际上

【android自定义控件】LinearLayout定义ActionBar样式

其实大家看到都ActionBar说白了,就是自定义的一个Linearlayout或者RelatedLayout:今天就练练LinearLayout 自定义. LinearLayout自定义方法有多种: 1.自定义xml布局,然后加载布局,自定义一个View继承LinearLayout 2.在自定义控件中声明它的所有子元素,然后在Layout文件中像使用LinearLayout一样去进行布局, 第二种比较烦 ,它需要在Layout文件中定义好子元素之后,要在代码 onFinishInflate()

android 实现带清除效果的EditText(附带抖动效果)

Android一直没有提供类似于ios中自带清除效果的输入框(ios只要只要添加属性即可实现),所以在Android当中 想要实现此效果就需要使用自定义控件的方式实现. 思路:可以使用一个Linearlayout里面横向布局一个EditText和一个删除的图片,监听输入框的焦点和文字变化,设置图片的显隐和点击清除事件.但是这么做些弊端,首先增加了UI布局的层级结构不利于UI结构的优化而且可能会出现文字过长遮挡住图片的情况.所以采用自定义控件继承于EditText,使用getCompoundDra

Android如何创建背景透明的Dialog

一:控制Dialog 的背景方法:1.定义一个无背景主题主题<!--去掉背景Dialog--> <style name="NobackDialog" parent="@android:style/Theme.Dialog"> <item name="android:windowBackground">@color/no_back</item> </style> 2.创建Dialog di

(转)Android 自定义 spinner (背景、字体颜色)

Android 自定义 spinner (背景.字体颜色) (2012-07-04 17:04:44)   1.准备两张图片,并做好9.png 2.在drawable中定义spinner_selector.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" &

Android开发新手需知EditText属性解析

麦子学院android开发谭老师说:才开始学习android开发时,android开发的基本知识是必须了解.记住的.熟练的.在android开发中,EditText继承TextView,所以EditText具有TextView的属性特点,下面主要介绍一些EditText的特有的输入法的属性特点 android:layout_gravity="center_vertical":设置控件显示的位置:默认top,这里居中显示,还有bottom android:hin:Text为空时显示的文字