`
zyj7261349
  • 浏览: 26617 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
文章分类
社区版块
存档分类
最新评论

代码设置EditText不可编辑

阅读更多
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;
				}
			} });
		}
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics