这里只是一个简单的问题。我在scrollview中有一个edittext。这是关于edittext的代码
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="899dp" >
<LinearLayout
android:id="@+id/LL1"
android:layout_width="match_parent"
android:layout_height="320dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivMainPic"
android:layout_width="match_parent"
android:layout_height="320dp"
/>
</LinearLayout>
<TextView
android:id="@+id/tvDescription"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_below="@+id/LL1"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="@string/strDescription"
android:textSize="14dp"
android:layout_marginLeft="30dip"
android:layout_marginRight="30dip"/>
<TextView
android:id="@+id/tvCondition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tvDescription"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dip"
android:text="@string/strCondition"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/btnGetOffer"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="40dip"
android:layout_below="@+id/etConditionBody"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="@string/strGetOffer"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:background="@drawable/roundedcorner"
android:textStyle="bold" />
<TextView
android:id="@+id/etConditionBody"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="@+id/tvCondition"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:gravity="top"
android:paddingBottom="20dip"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:paddingTop="20dip"
android:scrollbars="vertical"
android:background="@drawable/edittextborder"/>
</RelativeLayout>
</ScrollView>
现在我正在做的是使它可滚动和不可编辑的edittext,因为我在我的数据库上发布它的文本。 我尝试使用此代码
etConditionBody= (EditText) findViewById(R.id.etConditionBody);
etConditionBody.setScroller(new Scroller(getBaseContext()));
etConditionBody.setMaxLines(8);
etConditionBody.setVerticalScrollBarEnabled(true);
etConditionBody.setMovementMethod(new ScrollingMovementMethod());
etConditionBody.setText("wew \n wew2 \n wew3 \n wew4 \n wew \n wew2 \n wew3 \n wew4 \n wew \n wew2 \n wew3 \n wew4 \n wew3 \n wew4 \n wew \n wew2 \n wew3 \n taena");
但它不是滚动,只是将我的行限制为8
我使用了基于此链接的代码 how to enable vertical scrollbar of an edittext programmatically How to make edittext in Android scrollable? 但它无法正常工作
答案 0 :(得分:2)
为此使用TextView而不是edittext。您可以使用滚动视图进行滚动,例如
<ScrollView
android:layout_width="match_parent"
android:layout_height="300dp">
<TextView
android:id="@+id/tvConditionBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</ScrollView>
答案 1 :(得分:0)
不是指定Max行,而是减少EditTextHeight。
例如:
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/etConditionBody"
android:layout_width="fill_parent"
android:layout_height="300dp" >
</EditText>
</LinearLayout>
代码:
etConditionBody = (EditText) findViewById(R.id.etConditionBody);
etConditionBody.setScroller(new Scroller(getBaseContext()));
etConditionBody.setVerticalScrollBarEnabled(true);
etConditionBody.setMovementMethod(new ScrollingMovementMethod());
etConditionBody
.setText("wew \n wew2 \n wew3 \n wew4 \n wew \n wew2 \n wew3 \n wew4 \n wew \n wew2 \n wew3 \n wew4 \n wew3 \n wew4 \n wew \n wew2 \n wew3 \n taena");
答案 2 :(得分:0)
当您放置etConditionBody.setMaxLines(8);
时,它将始终只显示8行,因为它会覆盖layout_height参数。删除setMaxLines并最小化layout_height。它会滚动然后。
答案 3 :(得分:0)
使用以下代码
更新相应的java文件 EditText etText = (EditText) findViewById(R.id.etText);
dwEdit.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
if (view.getId() ==R.id.DwEdit) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction()&MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});