كيفية تكرار Android: requitable = "false" في الكود؟
-
20-08-2019 - |
سؤال
في التصميم ، يمكنك تعيين ملف EditText
عنصر واجهة المستخدم ليكون غير قابلة للتحرير عبر android:editable attribute
.
كيف يمكنني القيام بذلك في الكود؟ أنا بحاجة لجعل EditText
أداة أن تكون قابلة للتحرير اعتمادًا على الظروف.
المحلول
أعتقد InputFilter
هذا يرفض كل التغييرات هو حل جيد:
editText.setFilters(new InputFilter[] {
new InputFilter() {
public CharSequence filter(CharSequence src, int start,
int end, Spanned dst, int dstart, int dend) {
return src.length() < 1 ? dst.subSequence(dstart, dend) : "";
}
}
});
يحرر: اقترح Dlazar (أدناه) تغيير return
إلى dst.subSequence(dstart, dend)
للتغلب على السلوك الذي يزيل الكلمات.
نصائح أخرى
editText.setFocusable(false);
editText.setClickable(false);
هذا ضمان EditText
لا يمكن اختيار التحكم وتركيزه ، لذلك لا يمكن تحريره.
لقد جربت هذا بنفسي ،
لتعطيل نص التحرير:
.setFocusable(false);
هذا أيضا تعيين setFocusableIntouchMode إلى false!
لتمكين نص التحرير:
setFocusableInTouchMode(true);
هذا يضبط أيضا setFocusable إلى true.
The best way to do this is with this single line of code:
textView.setKeyListener(null);
The docs say for this method:
Sets the key listener to be used with this TextView. This can be null to disallow user input.
android:editable="false"
android:inputType="none"
in your xml or
EditText mEdit = (EditText) findViewById(R.id.yourid);
mEdit.setEnabled(false);
or
EditText mEdit = (EditText) findViewById(R.id.yourid);
mEdit.setKeyListener(null);
You can try this:
mEditText.setFocusable(false);
mEditText.setClickable(false);
mEditText.setFocusableInTouchMode(false);
mEditText.setLongClickable(false);
mEditText.setInputType(InputType.TYPE_NULL);
This will completely disable EditText, disable long press if you don't want user to open edit text options.
I do not see a related method for that attribute in the EditText
class. However, there are other similar things you could use such as
android:focus/setFocusable(boolean)
or create another TextView whose
android:editable="false"
and use setVisiblilty()
to switch between the
editable and not editable views. If you use View.GONE
the user will
never know there are two EditTexts.
If your feeling ambitious you could probably do something with the EditText's onTextChanged
listener like having it react with a setText
.
[Posting a new answer, since I can't comment on Josef's answer.]
The input filter works fine, but it has a subtle bug in it: typing over a selection will delete all the text.
For example, say you have the text "foo"
in the EditText
. If you select it all (e.g., by double-clicking on it) and type 'a'
, the text will disappear. This is because the InputFilter
will be called as:
filter("a", 0, 1, "foo", 0, 3);
The proposed input filter will return the empty string in this case (because src.length() < 1
is false
), which explains the missing text.
The solution is to simply return dst.subSequence(dstart, dend)
in the filter function. This will work fine even for deletions.
Have you tried setText(java.lang.CharSequence, android.widget.TextView.BufferType) ? It's described as:
Sets the text that this TextView is to display (see setText(CharSequence)) and also sets whether it is stored in a styleable/spannable buffer and whether it is editable.
(emphasis mine)
I guess
Edittext.setEnabled(false);
through code
and
android:enabled="false"
through xml.Also check this post on SO.
They should work and you can enable again programatically Edittext.setEnabled(true);
I wanted to also point out an alternative solution that works nicely if you are creating new instances of an EditView. You can override the method getDefaultEditable() as suggested by the docs to return false. E.g.
EditText view = new EditText(DiscountCalculator.this) {
public boolean getDefaultEditable() {
return false;
}
};
The only solution I have found for this scenario is to create 2 layouts. One is editable and one is not. You may have to create more than 2 layouts based on various conditions. Store the conditions in SharedPreferences or other means and load the relevant layout based on conditions after restarting the Activity. Here's an example:
in onCreate() of the activity:
configuration = new Configuration(this.getSharedPreferences(Configuration.SHARED_PREFERENCES_FILE_NAME, Context.MODE_PRIVATE));
manualSettingsMode = configuration.isManualSettingsMode();
if(manualSettingsMode){
setContentView(R.layout.editableconfigurationsettings);
}else {
setContentView(R.layout.configurationsettings);
}
The activity can be restarted based on testing for condition and calling functions as:
private void setManualEditing(){
configuration.set_isManualSettingsMode(true);
this.recreate();
}
private void setAutoEditing(){
configuration.set_isManualSettingsMode(false);
this.recreate();
}
Hope this helps. There really has to be a better solution, but this is what I've been doing. Ideally, one would be able to do this on individual fields and not have to reload the activity / layouts. -bobby
I think the correct way to achieve the desired effect is:
mEditView.setText("my text", BufferType.NORMAL);
If you want to switch between editable and non-editable you can do the following:
// Switch to non-editable
mEditView.setText(mEditView.getText(), BufferType.NORMAL);
// Switch back to editable
mEditView.setText(mEditView.getText(), BufferType.EDITABLE);
try this:
mEditText.setFilters(new InputFilter[]{new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (XXX) {
return "";
} else {
return null;
}
}
}});