1. 程式人生 > >edittext的可編輯和不可編輯

edittext的可編輯和不可編輯

如果讓EditText不可編輯在xml實現很簡單Android:editable 可是在程式碼中卻沒有setEditable

只能使用

editText.setFilters(new InputFilter[] { new InputFilter() {
 @Override
 public CharSequence filter(CharSequence source, int start,
  int end, Spanned dest, int dstart, int dend) {
   return source.length() < 1 ? dest.subSequence(dstart, dend) : "";
 }
} });

佈局檔案設定:

<?xml version="1.0" encoding="utf-8"?>
<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" tools:context="com.example.ubuntu.myapplication.MainActivity"> <!--<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Hello JEJ World!" /> --> <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="hello_world" android:inputType="text"/> <Button android:layout_below="@id/edit_text" android:id="@+id/button_onluck" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout> activity實現
package com.example.ubuntu.myapplication;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View.OnKeyListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText  edittext = null;
    private Button button_unluck = null;
    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edittext = (EditText) findViewById(R.id.edit_text);
        int lenth = edittext.getText().toString().length();
        android.util.Log.d("jiangerji" , "out lenth =" + lenth);
        edittext.addTextChangedListener(mTextEditorWatcher);
        button_unluck = (Button) findViewById(R.id.button_onluck);
        button_unluck.setOnClickListener(this);
        edittext.setOnKeyListener(new OnKeyListener(){

            @Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_DEL){
                    edittext.setFilters(new InputFilter[] { new InputFilter() {
                        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                            return null;
                        }
                    }
                    });
                }
                return false;
            }

        });
    }
    Handler mUiHandler = new Handler(){
        @Override
public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int lenth = (edittext.getText().toString()).getBytes().length;
            switch (msg.what)
            {
                case 1:

                    if(lenth > 30){
                        android.util.Log.d("jiangerji" , "msg lenth =" + lenth);
                        edittext.setFilters(new InputFilter[] { new InputFilter() {
                            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                                return source.length() < 1 ? dest.subSequence(dstart, dend) : "";
                            }
                        }
                        });
                    }
                    break;
                case 2:
                        android.util.Log.d("jiangerji" , "msg lenth =" + lenth);
                        edittext.setFilters(new InputFilter[] { new InputFilter() {
                            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                                return null;
                            }
                        }
                        });
                    break;
                default:
                    break;
            }
        }
    };
    private final TextWatcher mTextEditorWatcher = new TextWatcher() {
        private int mStart;

        private Runnable mRunnable = new Runnable() {
            public void run() {

                    Toast.makeText(MainActivity.this, "good ",
                            Toast.LENGTH_SHORT).show();
            }
        };

        @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            Log.d("jiangerji", "beforeTextChanged count = " + count + " after =" + after);
        }

        @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
            int lenth = (edittext.getText().toString()).getBytes().length;
            if (lenth > 30) {
                mUiHandler.removeCallbacks(mRunnable);
                mUiHandler.postDelayed(mRunnable, 200);
                mUiHandler.sendEmptyMessage(1);
                Log.d("jiangerji", "afterTextChanged lenth=" + lenth + "s");
            }
        }

        @Override
public void afterTextChanged(Editable s) {
            Log.d("jiangerji", "afterTextChanged s =" + s);

        }
    };

    @Override
public void onClick(View v) {
        if (v.equals(button_unluck)){
            mUiHandler.sendEmptyMessage(2);
        }
    }
}


參考文件:http://my.oschina.net/u/246578/blog/210534