Allow EditText to Scroll while Disabled

梦想的初衷 提交于 2019-12-23 11:55:53

问题


I have an EditText set up as follows:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="beforeDescendants"
        android:focusableInTouchMode="true"
        android:orientation="vertical" >

        <EditText
            android:layout_width="match_parent"
            android:layout_height="375dp"
            android:ems="10"
            android:gravity="top">
        </EditText>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</ScrollView>

At a certain point in the program, after text has been typed, the EditText becomes disabled by the following code:

edittext.setFocusable(false);
edittext.setEnabled(false);

The problem is that, once the EditText is disabled, I can't scroll up and down to see what I've written. How do I disable more typing, but still allow for review of what's been written?


回答1:


Try to set android:inputType="textMultiLine" on your EditText. Even if I already said in comments: there is a bug on lower API, but with the fixed height maybe it will work.

However, if this doesn't work, you should create another view as TextView with same properties of the EditText and also with visibility to gone, as follow:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="beforeDescendants"
        android:focusableInTouchMode="true"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/edittext"
            android:layout_width="match_parent"
            android:layout_height="375dp"
            android:ems="10"
            android:gravity="top" >
        </EditText>

        <TextView
            android:id="@+id/textview"
            android:layout_width="match_parent"
            android:layout_height="375dp"
            android:ems="10"
            android:gravity="top"
            android:visibility="gone" >
        </TextView>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</ScrollView>  

Then, when you handle the setOnKeyListener method with KeyEvent.ACTION_DOWN and KeyEvent.ACTION_ENTER, you can copy your text inside the textview and remove your edittext:

edittext.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
            String st = edittext.getText().toString();
            textview.setVisibility(View.VISIBLE);
            textview.setText(st);
            return true;
        }
        return false;
    }
});

And you can do a reverse method if you need to rewrite your text.
Hope this helps.



来源:https://stackoverflow.com/questions/22799770/allow-edittext-to-scroll-while-disabled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!