首页 文章

如何在Android中通过XML编辑EditText?

提问于
浏览
270

任何人都可以告诉我如何通过XML使 EditText 无法编辑?我尝试将 android:editable 设置为 false ,但是

  • 不推荐使用;和

  • 它不起作用 .

24 回答

  • 13

    正如其他答案中所提到的,你可以做一个 setEnabled(false) 但是因为你问的是如何通过XML设置它,这里是如何做到的 .

    将以下属性添加到 EditText

    android:enabled="false"
    
  • 24

    我遇到了同样的问题,但经过观察后发现 android:editable="false" 仅在Edittext中给出inputtype( android:inputType="ANY_VALUE" )为 not 时才起作用 .

    从EditText中删除inputType并使用editable = false,它的工作正常 .

  • 1

    尝试

    .setInputType(InputType.TYPE_NULL);
    
  • 55

    试试这个代码 . 它在我的项目中工作,因此它将在您的项目中工作 .

    android:editable="false"
    
  • 7

    使用这个简单的代码:

    textView.setKeyListener(null);
    

    有用 .

    Edit : To add KeyListener later, do following

    1:将键监听器设置为 textView 的标签

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

    2:从标签获取关键监听器并将其设置回 textView

    textView.setKeyListener((KeyListener) textView.getTag());
    
  • 1

    将其添加到EditText xml文件中:

    <EditText ...
            android:clickable="false" 
            android:cursorVisible="false" 
            android:focusable="false" 
            android:focusableInTouchMode="false">
    </EditText>
    

    它将起到与 android:editable="false" 相同的效果 . 为我工作,希望它也适合你 .

  • 6

    让你的Edittext成为

    EditText editText;
    

    editText.setKeyListener(null);

    会工作,但它只是没有听到钥匙 .

    用户可以在edittext上看到光标 .

    你可以试试 editText.setEnabled(false);

    这意味着用户无法看到光标 . 简单地说它将成为 TextView .

  • -10

    禁用XML(一行):

    android:focusable="false"
    

    如果需要,也可以从Java重新启用(也是一行):

    editText.setFocusableInTouchMode(true);
    
  • 375

    他们将“可编辑”弃用,但没有在inputType中提供相应的工作 .

    顺便说一句,inputType =“none”有效吗?就我看来,使用与否使用并没有任何区别 .

    例如,默认的editText被称为单行 . 但是你必须选择一个inputType作为单行 . 如果选择“无”,它仍然是多行的 .

  • 3

    正如你提到的android:editable已被弃用 . 应该使用android:inputType = "none"但由于错误它不起作用(https://code.google.com/p/android/issues/detail?id=2854

    但是你可以通过使用可聚焦来实现同样的目的 .

    通过XML:

    <EditText ...
            android:focusable="false" 
    </EditText>
    

    来自代码:

    ((EditText) findViewById(R.id.LoginNameEditText)).setFocusable(false);
    
  • 8

    如果要单击它,但不想编辑它,请尝试:

    android:focusable="false"
    
  • 1

    这个组合对我有用:

    <EditText ... >
        android:longClickable="false"
        android:cursorVisible="false"
        android:focusableInTouchMode="false"
    </EditText>
    
  • 357
    <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);
    
  • 1

    如果你想在java代码中执行它,只需使用此行禁用它:

    editText.setEnabled(false);
    

    这样就可以了:

    editText.setEnabled(true);
    
  • 24

    我试着这样做:

    textView.setInputType( InputType.TYPE_NULL );
    

    这应该工作,但对我来说它没有 .

    我完成了这段代码:

    textView.setKeyListener(new NumberKeyListener() {
        public int getInputType() {
            return InputType.TYPE_NULL;
        }
    
        protected char[] getAcceptedChars() {
            return new char[] {};
        }
    });
    

    哪作得很好 .

  • 1

    只需使用 android:enabled="false" . 这也会给出 EditText 的外观,使其看起来不可编辑 .

  • 15

    您可以使用 android:editable="false" ,但我建议您使用 setEnabled(false) ,因为它为用户提供了无法编辑控件的直观线索 . 所有禁用的小部件都使用相同的视觉提示,并且一致性良好 .

  • -1

    当我希望活动不关注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>
    
  • 12

    我尝试过以下方法:

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

    最好的祝福,

    欢迎评论,

    约翰戈切

  • 3

    除了@mahe madi,你也可以尝试这个

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

    此方法将隐藏光标,失去焦点,更重要的是将文本颜色设置为黑色,表现得像TextView .

    要恢复返回editText,只需执行此操作即可获得EditText的所有属性 .

    editText.setEnabled(true);
    

    希望能帮助到你 :)

  • 1

    如果我想编辑,我使用 EditText.setFocusable(false) 并再次设置为true .

  • 5

    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" />
    
  • 1

    试试这个 :

    android:inputType="none"
    
  • 5

    这种方式为我做了工作,我可以选择并复制到剪贴板,但我不能修改或粘贴 .

    android:enable="true"
    android:textIsSelectable="true"
    android:inputType="none"
    

相关问题