您的位置:首页 > 其它

自定义的MyEditText,自带最多输入多少字符功能

2016-07-18 20:19 309 查看

自定义的控件,MyEditText。自带最多输入多少字符的功能



废话不多少,直接上代码吧

/**
* @author  张玉水
*/
public class MyEditText extends RelativeLayout {

//private String NAMESPACE="http://schemas.android.com/apk/res/com.babyrun.mmsh";
private int maxLength=200;
private  int minLines=5;
private  Context mContext;
private EditText et;
private TextView tv;

public MyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.mContext=context;
//下边是引入域名空间用到代码段,在studio中,不能像上边的NAMESPACE一样引入命名空间,所以
//用setCustomAttributes()方法得到自定义的属性
//      if (attrs!=null) {
//          maxLength = attrs.getAttributeIntValue(NAMESPACE, "maxlength", 200);
//          minLines = attrs.getAttributeIntValue(NAMESPACE, "minlines", 5);
//      }
if (attrs!=null) {
setCustomAttributes(attrs);
}

init(context);
}

public MyEditText(Context context, AttributeSet attrs) {
this(context, attrs,0);
this.mContext=context;

}

public MyEditText(Context context,int maxlenth) {
super(context);
this.mContext=context;
maxLength=maxlenth;
init(context);

}

private void setCustomAttributes(AttributeSet attrs) {
TypedArray a = mContext.obtainStyledAttributes(attrs,R.styleable.MyEditText);
//最大长度
maxLength = a.getInt(R.styleable.MyEditText_maxlength,200);
//最小高度
minLines = a.getInt(R.styleable.MyEditText_minlines, 5);

}

public void hint(String s){
et.setHint(s);
}
//初始化
private void init(Context context) {
View view = View.inflate(context, R.layout.myedittext, null);
et = (EditText) view.findViewById(R.id.et);
et.setMinLines(minLines);
// et.setInputType(InputType.TYPE_CLASS_TEXT); //输入类型
et.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)}); //最大输入长度
tv = (TextView) view.findViewById(R.id.tv);
int length = et.getText().toString().length();
tv.setText(length+"/"+maxLength);
et.addTextChangedListener(new TextWatcher() {

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

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {

}

@Override
public void afterTextChanged(Editable s) {
int length = et.getText().toString().length();
tv.setText(length+"/"+maxLength);
}
});

addView(view);

}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
//实现类似于Edittext的方法
public void setText(String s){
et.setText(s);
}
public Editable getText(){
return et.getText();
}
public void setEndable(boolean flag){
et.setEnabled(flag);
}

}


R.layout.myedittext文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip"
android:background="@color/white" >
<EditText
android:id="@+id/et"
style="@style/normal_text_dark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dip"
android:layout_marginLeft="@dimen/view_space"
android:layout_marginRight="5dp"
android:layout_marginTop="15dip"
android:gravity="top"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:paddingRight="15dp"
android:paddingBottom="45dp"
android:background="@drawable/choose_button_bg"
android:longClickable="false"
/>
<TextView
android:id="@+id/tv"
android:layout_marginBottom="15dp"
android:layout_alignBottom="@id/et"
android:layout_marginRight="25dp"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
style="@style/normal_text_dark"
android:layout_height="wrap_content"
android:text="0/200"
/>
</RelativeLayout>


@drawable/choose_button_bg文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<padding
android:bottom="10dip"
android:left="10dip"
android:right="10dip"
android:top="10dip" />

<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp" />

<solid android:color="#ffffff" />

<stroke
android:width="0.1dip"
android:color="@color/activity_text_dark" />

</shape>


下边是属性的命名,在attrs.xml 文件中。

<!-- 自定义控件myEditView的最大长度 -->
<declare-styleable name="MyEditText">
<attr name="maxlength" format="integer" />
<attr name="minlines" format="integer" />
</declare-styleable>


重要的文件,都基本列完了,剩下的不太重要的,读者可以根据需要自己设置

最后是使用的方法,在布局文件中 的使用

<linnerlayout
...
xmlns:myedittext="http://schemas.android.com/apk/res-auto">
...
<com.babyrun.mmsh.widget.MyEditText
android:id="@+id/service_location_myet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
myedittext:minlines="3"
myedittext:maxlength="50" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: