Allow EditText to Scroll while Disabled

2019-06-27 05:12发布

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条回答
我命由我不由天
2楼-- · 2019-06-27 05:43

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.

查看更多
登录 后发表回答