标签:
内容、输入法、焦点
1、弹出窗口时自动让控件获取焦点,并弹出输入法。注意要在onCreate中法中设置,不能在onResume中设置。et.requestFocus();new Timer().schedule(new TimerTask() {//Timer是一种定时器工具,用来在一个后台线程计划执行指定任务。//它可以计划执行一个任务一次或反复多次。TimerTask是一个抽象类,它的子类代表一个可以被Timer计划的任务。@Overridepublic void run() {//300毫秒后,通过输入法管理器,切换输入法的显示状态InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);//切换输入法}}, 300);2、不允许手动换行,但允许自动换行mEditText.setOnEditorActionListener(new OnEditorActionListener() {@Overridepublic boolean onEditorAction(TextView v, int actionId, KeyEvent event) {return (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);}});3、内容监听器TextWatcher,定义的方法onTextChanged的参数的含义public void onTextChanged(CharSequence charSequence, int start, int before, int count) {//text 改变后输入框中的字符串//start 内容是从哪个位置开始改变的,第一个位置为0//before 若为0,代表字符增加了,增加的个数为count;若不为0,代表字符减少了,减少的个数就是before的值Log.i("bqt", charSequence + "--" + start + "--" + before + "--" + count);onLimitLenChanged(temp.length());}
带删除按钮的EditText-布局
<?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" ><EditTextandroid:id="@+id/et_view"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:background="#fff"android:gravity="center"android:hint="请输入账号"android:paddingBottom="14dp"android:paddingTop="14dp"android:singleLine="true"android:textColor="#000"android:textSize="13sp" /><ImageViewandroid:id="@+id/iv_clean"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_alignBottom="@id/et_view"android:layout_alignRight="@id/et_view"android:layout_alignTop="@id/et_view"android:layout_marginRight="5dp"android:paddingLeft="10dp"android:paddingRight="10dp"android:src="@drawable/delete"android:visibility="gone" /></RelativeLayout>
带删除按钮的EditText-代码
public class EditCancel extends FrameLayout implements TextWatcher {
private EditText et_view;//内容private ImageView iv_clean;//清空按钮private ITextChangeListener mTextChangeListener;//监听public EditCancel(Context context) {super(context);initView(context);}public EditCancel(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);initView(context);}public EditCancel(Context context, AttributeSet attrs) {super(context, attrs);initView(context);}private void initView(Context context) {inflate(context, R.layout.custom_editview, this);et_view = (EditText) findViewById(R.id.et_view);iv_clean = (ImageView) findViewById(R.id.iv_clean);et_view.addTextChangedListener(this);iv_clean.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {et_view.setText("");}});}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) {if (s.length() > 0) iv_clean.setVisibility(View.VISIBLE);else iv_clean.setVisibility(View.GONE);if (mTextChangeListener != null) mTextChangeListener.onTextChange();}public EditText getEditText() {return et_view;}public ImageView getIvClean() {return iv_clean;}public ITextChangeListener getTextChangeListener() {return mTextChangeListener;}public void setTextChangeListener(ITextChangeListener mTextChangeListener) {this.mTextChangeListener = mTextChangeListener;}}public interface ITextChangeListener {void onTextChange();}
标签:
原文地址:http://www.cnblogs.com/baiqiantao/p/5389592.html