I have an EditText on my layout. Below are the attributes I currently have:
<EditText
android:id="@+id/entryIdea"
android:layout_width="fill_parent"
android:layout_height="225sp"
android:gravity="top"
android:background="@android:drawable/editbox_background"
android:scrollbars="vertical"/>开发者_开发问答;
However, I can see the scrollbar but can't scroll it with mouse/touch. I thought that it may works if I put the corresponding listener since it works on TextView. Apparently, it isn't.
EditText et = (EditText)findViewById(R.id.entryIdea);
et.setMovementMethod(new ScrollingMovementMethod());
Can you guys help me on this?
Thank you so much in advance. Sammy
In your XML try setting the EditText
height not in layout_height
, but rather use android:lines
attribute (btw, using sp is not usually a good practice when setting a size for anything except font size. Using dp/dip is more natural in this case).
Meanwhile set layout_height
to wrap_content
. Otherwise the XML you presented (with the changes I've mentioned) worked fine for me even without specifying movement method in the code.
And of course, the scroll bar will appear when the actual amount of lines of text in the EditText is larger than that, indicated in android:lines attribute.
refer this link
EditText dwEdit = (EditText) findViewById(R.id.DwEdit);
dwEdit.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
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;
}
});
editText1.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
if (view.getId() ==R.id.editText1) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction()&MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
use this:
android:maxLines="5"
attribute for your xml file. Then the scrollbars attribute will work.
editText1.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
if (view.getId() ==R.id.editText1) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction()&MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
in xml file use:
android:maxLines="5" android:scrollbars = "vertical"
and in .java file add
edt_text.setMovementMethod(new ScrollingMovementMethod());
your xml file use:
android:maxLines="5"
精彩评论