今天在做项目的时候,要想实现一个将EditText变成不可编辑的状态,通过查找博客,发现一个好方法,对于单独的EditText控件我们可以单独设置
1.首先想到在xml中设置Android:editable="false",但是如果想在代码中动态设置可编辑状态,没有找到对应的函数。
2。然后尝试使用editText.setFocusable(false);和editText.setEnabled(false);设置不可编辑状态;editText.setFocusable(true);和 editText.setEnabled(true);设置可编辑状态。
发现在editText.setFocusable(false);和editText.setEnabled(false);时不可编辑,但是editText.setFocusable(true);和 editText.setEnabled(true);也是不可编辑的,感觉这个时候EditText控件高亮度了,但是没有焦点
3.最后尝试使用editText.setFocusable(false);和editText.setFocusableInTouchMode(false);设置不可编辑状态;editText.setFocusableInTouchMode(true);editText.setFocusable(true);editText.requestFocus();设置可编辑状态
实现方式:
android:focusable="true" android:focusableInTouchMode="true"
4.如果让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) : "";
}
} });
package com.battlefart; import android.app.Activity; import android.text.InputFilter; import android.text.Spanned; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class Myfarts_details extends Activity{ private Button Edit_button; private EditText fart_name; private EditText last_meal; private EditText comments; private String fart_name_string; private String last_meal_string; private String comments_string; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_farts); //lock(); Edit_button=(Button)findViewById(R.id.button_edit); fart_name=(EditText)findViewById(R.id.fart_name); last_meal=(EditText)findViewById(R.id.last_meal); comments=(EditText)findViewById(R.id.comments); lock(); Edit_button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Unlock(); fart_name_string = fart_name.getText().toString(); last_meal_string = last_meal.getText().toString(); comments_string=comments.getText().toString(); SharedPreferences preference = getSharedPreferences("player_information",Context.MODE_PRIVATE); Editor edit = preference.edit(); edit.putString("my_fart_name",fart_name_string); edit.putString("my_last_meal",last_meal_string); edit.putString("my_comments",comments_string); edit.commit(); } }); } //让EditText变成可编辑状态 private void Unlock() { fart_name.setFilters(new InputFilter[] { new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { return null; } } }); last_meal.setFilters(new InputFilter[] { new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { return null; } } }); comments.setFilters(new InputFilter[] { new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { return null; } } }); } //让EditText变成不可编辑状态 private void lock(){ fart_name.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) : ""; } } }); last_meal.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) : ""; } } }); comments.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) : ""; } } }); } }
(申明下,这个我是在坛子中看到的,本人测试有效) 这样做就可以在java代码中动态设置EditText的编辑状态了,但这个无法解决用户体验的问题,软键盘依然会打开,但无法输入,但可以选择内容。
通过这样方式试试吧,自定义一个类继承EditText,
如:
package custom.widget; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.EditText; public class CustomEditText extends EditText { public CustomEditText(Context context,AttributeSet attrs) { super(context,attrs); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub return true; } } //然后在页面布局文件使用自定义的EditText <custom.widget.CustomEditText android:layout_width="fill_parent" android:layout_height="wrap_content" />