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) : "";
 }
} });

 

import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * Class which shows how to lock and unlock EditText component
 *
 * @author FaYnaSoft Labs
 */
public class Main extends Activity {
	private EditText editText;
	private boolean value = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		editText = (EditText) findViewById(R.id.textId);
		editText.setText("EditText component");
		Button b = (Button) findViewById(R.id.btnId);
		b.setText("Lock/Unlock");
		b.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (value) {
					value = false;
				} else {
					value = true;
				}
				lockUnlock(value);
			}
		});
	}

	/**
	 * Method which locks and unlocks editText component
	 * @param value our boolean value which using in or if operator
	 */
	private void lockUnlock(boolean value) {
		if (value) {
			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)
							: "";
				}
			} });
		} else {
			editText.setFilters(new InputFilter[] { new InputFilter() {
				@Override
				public CharSequence filter(CharSequence source, int start,
						int end, Spanned dest, int dstart, int dend) {
					return null;
				}
			} });
		}
	}
}

 

你可能感兴趣的:(android,xml,OS)