【问题标题】:edit text field not getting edited编辑文本字段未被编辑
【发布时间】:2016-02-27 23:13:00
【问题描述】:

我有一个包含EditTextButton 的活动。 EditText 中的数据一开始是不可编辑的,但是当按下 Button 时它变成可编辑的。我曾尝试在 xml 布局中使用 android:editable="false",但它不起作用。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: android android-edittext


    【解决方案1】:

    您需要先在 xml 布局中将 TextEdit 可编辑属性设置为 false :

    <EditText
        ...
        android:id="@+id/input"
        android:clickable="false" />
    

    然后在单击按钮时,将 TextEdit 设置为可编辑:

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        input = (EditText) findViewById(R.id.input);
        button = (Button) findViewById(R.id.button);
    
        button.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                input.setClickable(true);
            }
    
        });
    }
    

    【讨论】:

    • 嘿,编辑文本字段没有 setEditable() 函数。我可以这样做吗?
    • 糟糕。不幸的是,editable 属性仅通过 XML 以编程方式公开,您应该使用 setEnabledsetFocusablesetClickable。但是,使用这些方法时,您将无法选择和复制文本...代码已更新。
    【解决方案2】:
    editText.setEnabled(true); or editText.setEnabled(false);
    

    如果不能工作,那么Editable false

    【讨论】:

      【解决方案3】:
      <EditText
          ...
          android:id="@+id/urId"
          android:clickable="True" />
      

      这对我有用,试试你的运气。

      【讨论】:

      • 欢迎来到 Stack Overflow!我已经编辑了你的答案。如果你想在这里显示代码,你应该缩进 4 个空格。这会自动在code markdown 中添加一行。
      猜你喜欢
      • 2012-01-05
      • 2015-12-03
      • 2011-05-26
      • 2012-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      相关资源
      最近更新 更多