How to make EditText not editable through XML in A

2019-01-08 03:09发布

Can anyone tell me how to make an EditText not editable via XML? I tried setting android:editable to false, but

  1. it is deprecated; and
  2. it didn't work.

24条回答
【Aperson】
2楼-- · 2019-01-08 03:27

When I want an activity to not focus on the EditText and also not show keyboard when clicked, this is what worked for me.

Adding attributes to the main layout

android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"

and then the EditText field

android:focusable="false"
android:focusableInTouchMode="false"

Here is an example:

<?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>
查看更多
▲ chillily
3楼-- · 2019-01-08 03:27

Try this :

android:inputType="none"
查看更多
手持菜刀,她持情操
4楼-- · 2019-01-08 03:28

This combination worked for me:

<EditText ... >
    android:longClickable="false"
    android:cursorVisible="false"
    android:focusableInTouchMode="false"
</EditText>
查看更多
趁早两清
5楼-- · 2019-01-08 03:29

I've tried the following:

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();

  }

});

but the problem was that if the edit text is the first in the form then it gets the focus and the pickCode() code which launches a new activity is called straight away. So I modified the code as follows and it seems to work quite well (except I cannot set the focus on the text edit but I don't need to):

itemCodeEditText.setFocusable(false);

this.itemCodeEditText.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {

    pickItem();

  }

});

Best Regards,

Comments welcome,

John Goche

查看更多
该账号已被封号
6楼-- · 2019-01-08 03:29

In addition to @mahe madi you can try this as well

editText.setEnabled(false);
editor.setTextColor(Color.BLACK);

This method will hide cursor, lose focus and more importantly set text color to black behaving like TextView.

And to revert Back to editText simply do this to gain all the properties of EditText.

editText.setEnabled(true);

Hope it Helps :)

查看更多
趁早两清
7楼-- · 2019-01-08 03:30

Use this simple code:

textView.setKeyListener(null);

It works.

Edit : To add KeyListener later, do following

1 : set key listener to tag of textView

textView.setTag(textView.getKeyListener());
textView.setKeyListener(null);

2 : get key listener from tag and set it back to textView

textView.setKeyListener((KeyListener) textView.getTag());
查看更多
登录 后发表回答