EditTextXML を使用して編集不可にする方法を教えてもらえますか?に設定android:editableしてみましたfalseが、
- 非推奨です。そして
- うまくいきませんでした。
EditTextXML を使用して編集不可にする方法を教えてもらえますか?に設定android:editableしてみましたfalseが、
回答:
次の簡単なコードを使用します。
textView.setKeyListener(null);できます。
編集:KeyListener後で追加するには、次の操作を行います
1:キーリスナーをタグに設定します textView
textView.setTag(textView.getKeyListener());
textView.setKeyListener(null);2:タグからキーリスナーを取得し、それを textView
textView.setKeyListener((KeyListener) textView.getTag());setFocusable()変更するとデフォルトの実装が変更される可能性があるとドキュメントが述べているためです。
                    これをEditText xmlファイルに追加します。
<EditText ...
        android:clickable="false" 
        android:cursorVisible="false" 
        android:focusable="false" 
        android:focusableInTouchMode="false">
</EditText>と同じ効果がありandroid:editable="false"ます。私のために働いた、それがあなたのためにも働くことを願っています。
TextInputLayout
                    Edittextを
EditText editText;editText.setKeyListener(null);  
動作しますが、キーを聞くだけではありません。
ユーザーはedittextにカーソルを表示できます。
あなたが試すことができます editText.setEnabled(false);
つまり、ユーザーはカーソルを見ることができません。簡単に言うとになりTextViewます。
他の回答で述べたように、あなたはそれを行うことができますsetEnabled(false)  が、XMLを介してそれを設定する方法を尋ねているので、ここでそれを行う方法を説明します。
次の属性をに追加しますEditText。
android:enabled="false"enabledは非推奨ではありません。editableです。
                    XMLから無効にする(1行):
 android:focusable="false"必要に応じて、Javaから再度有効にします(これも1行です)。
editText.setFocusableInTouchMode(true);「編集可能」を非推奨にしましたが、inputTypeで対応する機能を提供しませんでした。
ところで、inputType = "none"は効果がありますか?それを使用してもしなくても、私が見る限り、違いはありません。
たとえば、デフォルトのeditTextは1行と呼ばれます。ただし、それが単一行になるようにinputTypeを選択する必要があります。「なし」を選択しても、複数行のままです。
この組み合わせは私にとってうまくいきました:
<EditText ... >
    android:longClickable="false"
    android:cursorVisible="false"
    android:focusableInTouchMode="false"
</EditText>あなたが言ったようにandroid:editableは非推奨です。代わりにandroid:inputType = "none"を使用する必要がありますが、バグのために機能しません(https://code.google.com/p/android/issues/detail?id=2854)
ただし、focusableを使用して同じことを実現できます。
XML経由:
<EditText ...
        android:focusable="false" 
</EditText>コードから:
((EditText) findViewById(R.id.LoginNameEditText)).setFocusable(false);だけを使用してください   android:enabled="false"。これにより、がEditText表示され、編集できないように見えます。
<EditText
    android:id="@+id/txtDate"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="2dp"
    android:clickable="false"
    android:cursorVisible="false"
    android:gravity="center" />そして以下を使用してください:
txtDate.setKeyListener(null);Javaコードで実行する場合は、次の行を使用して無効にします。
editText.setEnabled(false);そしてこれを有効にする:
editText.setEnabled(true);EditTextにフォーカスせず、クリックしたときにキーボードも表示しないようにしたい場合、これがうまくいきました。
メインレイアウトに属性を追加する
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"次にEditTextフィールド
android:focusable="false"
android:focusableInTouchMode="false"次に例を示します。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_home"
    android:background="@drawable/bg_landing"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    tools:context="com.woppi.woppi.samplelapp.HomeActivity">
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/fromEditText"
        android:layout_weight="1"
        android:hint="@string/placeholder_choose_language"
        android:textColor="@android:color/white"
        android:textColorHint="@android:color/darker_gray"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:onClick="onSelectFromLanguage" />
</RelativeLayout>使用することもできますandroid:editable="false"がsetEnabled(false)、コントロールを編集できないというユーザーへの視覚的な手掛かりを提供するため、使用することをお勧めします。無効になっているすべてのウィジェットで同じビジュアルキューが使用され、一貫性は良好です。
私は以下を試しました:
codeEditText.setInputType(InputType.TYPE_NULL);
this.codeEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
  @Override
  public void onFocusChange(View v, boolean hasFocus) {
    if (hasFocus) {
      pickCode();
    }
  }
});
this.codeEditText.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    pickCode();
  }
});しかし問題は、エディットテキストがフォームの最初の場合、フォーカスが取得され、新しいアクティビティを起動するpickCode()コードがすぐに呼び出されることでした。だから私は次のようにコードを変更し、それはかなりうまくいくようです(テキスト編集にフォーカスを設定することはできませんが、必要はありません):
itemCodeEditText.setFocusable(false);
this.itemCodeEditText.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    pickItem();
  }
});宜しくお願いします、
コメント歓迎、
ジョン・ゴッシュ
EditText.setFocusable(false)編集したい場合は、使用して再度trueに設定します。
android:editableは非推奨であるため、代わりにinputTypeを使用してください。
   <EditText
        android:id="@+id/editText_x"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="70"
        android:inputType="none"
        android:hint="@string/enter_x" />ダイアログでこれを解決します。
    AlertDialog dialog;
    EditText _editID;
    TextView _txtID;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_principal);
        _txtID = (TextView) findViewById(R.id._txtID);
        dialog = new AlertDialog.Builder(this).create();
        _editID = new EditText(this);
        _editID.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED);
        dialog.setTitle("Numero do Registro:");
        dialog.setView(_editID);
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, "IR", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                _txtID.setText(_editID.getText());
                toId(Integer.parseInt((_editID.getText().toString())));
            }
        });
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "CANCELAR", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        _txtID.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _txtID.setText("_editID.getText()");
                //_editID.setText("");
                dialog.show();
            }
        });短い答え:  
私が使用android:focusable="false"しましたが、うまくいきました。クリックリスニングも機能しています。
試す
.setInputType(InputType.TYPE_NULL);このコードを試してください。それは私のプロジェクトで機能しているので、あなたのプロジェクトでも機能します。
android:editable="false"