読み取り専用のEditText
ビューを作成したい。このコードを実行するXMLはのようですが、コードで実行しandroid:editable="false"
たいと思います。
これどうやってするの?
android:editable
は非推奨となっていることにも注意してくださいEditText
。
android:inputType="none"
ありますandroid:textIsSelectable="true"
読み取り専用のEditText
ビューを作成したい。このコードを実行するXMLはのようですが、コードで実行しandroid:editable="false"
たいと思います。
これどうやってするの?
android:editable
は非推奨となっていることにも注意してくださいEditText
。
android:inputType="none"
ありますandroid:textIsSelectable="true"
回答:
あなたは場合はsetEnabled(false)
、あなたのeditText
(など、灰色)無効になります。エディターの視覚的な側面を変更したくない場合があります。
より煩わしくない方法は、を使用することsetFocusable(false)
です。
私はこれがあなたの最初の意図に近い質問に答えると信じています。
setFocusable(false)
(とさえsetFocusableInTouchMode(false)
してsetClickable(false)
)あなたはまだあなたがの値を変更することができeditText
、あなたの押しeditText
のために数百ミリ秒をし、選択Paste
しない限り、オプションをシステムクリップボードが空です。私は23である最新の利用可能なAPIでそれをテストしました。
XML
使用:
android:editable="false"
例として:
<EditText
android:id="@+id/EditText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:editable="false" />
android:inputType="none"
android:inputType="none"
ていますが、データはまだ編集可能です。
これは私にとってはうまくいきます:
EditText.setKeyListener(null);
editText.setInputType(InputType.TYPE_NULL);
ドキュメントに従ってこれはソフトキーボードが表示されるのを防ぎます。また、貼り付けを防止し、スクロールを許可し、ビューの視覚的側面を変更しません。ただし、これにより、ビュー内のテキストの選択とコピーもできなくなります。
私のテストから、setInputType
への設定TYPE_NULL
は、機能的には減価償却と同等のようandroid:editable="false"
です。また、android:inputType="none"
目立った影響はないようです。
android:inputType="none"
機能しなくなりました。
android:editable = "false"は非推奨になりました。したがって、これを使用して編集テキストを読み取り専用にすることはできません。
私は以下のソリューションを使用してこれを行いました。ここで私は使用しました
android:inputType = "none"
android:textIsSelectable = "true"
android:focusable = "false"
それを試してみてください:)
<EditText
android:id="@+id/et_newsgpa_university"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="@string/hint_educational_institute"
android:textSize="@dimen/regular_text"
android:inputType="none"
android:textIsSelectable="true"
android:focusable="false"
android:maxLines="1"
android:imeOptions="actionNext"/>
android:enabled="false"
良い選択肢かもしれません。
editText.setEnabled(false);
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) : "";
}
} });
これにより、編集できないEditTextフィルターが作成されます。まず、必要なテキストをeditTextフィールドに入力してから、このフィルターを適用する必要があります。
return dst.subSequence(dstart, dend);
この2行を書くだけで十分です。
yourEditText.setKeyListener(null);
yourEditText.setEnabled(false);
android:editable
設定されている場合、これTextView
に入力メソッドがあることを指定します。特に指定のない限り、テキスト形式です。の場合TextView
、これはデフォルトでfalseです。の場合EditText
、デフォルトではtrueです。
またはのboolean
いずれtrue
かの値でなければなりませんfalse
。
これは、このタイプの値を含むリソース(フォーム@[package:]type:name
)またはテーマ属性(フォーム)への参照でもある場合があります?[package:][type:]name
。
これは、編集可能なグローバル属性リソースシンボルに対応します。
関連メソッド
エディットテキストのonLongClickリスナーをオーバーライドして、コンテキストメニューを削除してみてください。
EditText myTextField = (EditText)findViewById(R.id.my_edit_text_id);
myTextField.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
return true;
}
});
このコードを使用してください:
editText.setEnabled(false);
editText.setTextColor(ContextCompat.getColor(context, R.color.black);
無効editText
にすると、読み取り専用の外観と動作が得られますが、もtext-color
灰色に変わるため、テキストの色を設定する必要があります。
これは私の実装です(少し長いですが、私にとっては便利です!):このコードを使用すると、EditViewを読み取り専用または通常にできます。読み取り専用の状態でも、ユーザーはテキストをコピーできます。バックグラウンドを変更して、通常のEditTextとは異なる外観にすることができます。
public static TextWatcher setReadOnly(final EditText edt, final boolean readOnlyState, TextWatcher remove) {
edt.setCursorVisible(!readOnlyState);
TextWatcher tw = null;
final String text = edt.getText().toString();
if (readOnlyState) {
tw = new TextWatcher();
@Override
public void afterTextChanged(Editable s) {
}
@Override
//saving the text before change
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
// and replace it with content if it is about to change
public void onTextChanged(CharSequence s, int start,int before, int count) {
edt.removeTextChangedListener(this);
edt.setText(text);
edt.addTextChangedListener(this);
}
};
edt.addTextChangedListener(tw);
return tw;
} else {
edt.removeTextChangedListener(remove);
return remove;
}
}
このコードの利点は、EditTextは通常のEditTextとして表示されますが、コンテンツは変更できないことです。戻り値は変数として保持し、読み取り専用状態から通常状態に戻すことができるようにする必要があります。
EditTextを読み取り専用にするには、次のように入力します。
TextWatcher tw = setReadOnly(editText, true, null);
そしてそれを通常のようにするために前のステートメントからtwを使います:
setReadOnly(editText, false, tw);
これは、上記の提案のいくつかを考慮に入れてうまくいきました。TextEditをフォーカス可能にしますが、ユーザーがクリックまたはフォーカスすると、選択のリストをPopupWindowに表示します。(奇抜なSpinnerウィジェットを置き換えます)。TextEdit xmlは非常に一般的です...
public void onCreate(Bundle savedInstanceState) {
....
fEditState = (EditText) findViewById(R.id.state_edit);
fEditState.setLongClickable(false);
fEditState.setKeyListener(null);
fEditState.setFocusable(true);
fEditState.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
showStatesPopup();
}
}
});
fEditState.setOnClickListener(new Button.OnClickListener(){
public void onClick(View arg0)
{
showStatesPopup();
}
});
....
}
private void showStatesPopup()
{
// fPopupWindowStates instantiated in OnCreate()
if (!fPopupWindowStates.isShowing()) {
// show the list view as dropdown
fPopupWindowStates.showAsDropDown(fEditState, -5, 0);
}
}
コントロールからテキストをコピーするだけで編集はできないようにする場合は、代わりにTextViewを使用して、テキストを選択可能にすることができます。
コード:
myTextView.setTextIsSelectable(true);
myTextView.setFocusable(true);
myTextView.setFocusableInTouchMode(true);
// myTextView.setSelectAllOnFocus(true);
xml:
<TextView
android:textIsSelectable="true"
android:focusable="true"
android:focusableInTouchMode="true"
...
/>
<!--android:selectAllOnFocus="true"-->
setTextIsSelectable
のドキュメントは言う:
このメソッドを呼び出してtextIsSelectableの値を設定すると、focusable、focusableInTouchMode、clickable、およびlongClickableの各フラグに同じ値が設定されます...
ただし、タッチ入力で機能させるには、focusableおよびfocusableInTouchModeを明示的にtrueに設定する必要がありました。
EditTextPreferenceを読み取り専用にしても、問題はありませんでした。
editTextPref.setSelectable(false);
これは、「概要」フィールドを使用して読み取り専用フィールドを表示する場合(アカウント情報を表示する場合などに便利)と組み合わせるとうまく機能します。http://gmariotti.blogspot.com/2013/01/preferenceactivity-preferencefragment.htmlから動的に取得された集計フィールドの更新
private static final List<String> keyList;
static {
keyList = new ArrayList<String>();
keyList.add("field1");
keyList.add("field2");
keyList.add("field3");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
for(int i=0;i<getPreferenceScreen().getPreferenceCount();i++){
initSummary(getPreferenceScreen().getPreference(i));
}
}
private void initSummary(Preference p) {
if (p instanceof PreferenceCategory) {
PreferenceCategory pCat = (PreferenceCategory) p;
for (int i = 0; i < pCat.getPreferenceCount(); i++) {
initSummary(pCat.getPreference(i));
}
} else {
updatePrefSummary(p);
}
}
private void updatePrefSummary(Preference p) {
if (p instanceof ListPreference) {
ListPreference listPref = (ListPreference) p;
p.setSummary(listPref.getEntry());
}
if (p instanceof EditTextPreference) {
EditTextPreference editTextPref = (EditTextPreference) p;
//editTextPref.setEnabled(false); // this can be used to 'gray out' as well
editTextPref.setSelectable(false);
if (keyList.contains(p.getKey())) {
p.setSummary(editTextPref.getText());
}
}
}
これをEdiTextView xmlファイルに設定します
android:focusable="false"
android:editable=""
推奨されていません、
設定
android:clickable="false"
android:focusable="false"
android:inputType="none"
android:cursorVisible="false"
「読み取り専用」にします。
ただし、ユーザーはフィールドに貼り付けたり、他の長押しアクションを実行したりできます。これを無効にするには、単にonLongClickListener()をオーバーライドします。
コトリンでは:
myEditText.setOnLongClickListener { true }
十分です。
これに対する私のアプローチは、次のようにカスタムTextWatcherクラスを作成しています。
class ReadOnlyTextWatcher implements TextWatcher {
private final EditText textEdit;
private String originalText;
private boolean mustUndo = true;
public ReadOnlyTextWatcher(EditText textEdit) {
this.textEdit = textEdit;
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (mustUndo) {
originalText = charSequence.toString();
}
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (mustUndo) {
mustUndo = false;
textEdit.setText(originalText);
} else {
mustUndo = true;
}
}
}
次に、有効になっているにもかかわらず読み取り専用にするフィールドにウォッチャーを追加するだけです。
editText.addTextChangedListener(new ReadOnlyTextWatcher(editText));