我正在开发一个应用程序,其中我是一个结构点。
根据我的应用程序要求,我在xml中创建了水平scrollview,然后在.java中创建了垂直scrollview:
// Vertical Scroll view in Linear layout
ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
然后我以编程方式创建了一个表视图,并将其添加到scrollview中。我创建了多行编辑文本并禁用它,因为我想在运行时设置文本并将其作为一行添加到表视图中..
EditText editText = new EditText(this);
editText.setId(1);
editText.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,0f));
editText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
editText.setGravity(Gravity.TOP|Gravity.LEFT);
editText.setHint("Comment");
editText.setSingleLine(false);
editText.setLines(5);
editText.setMaxLines(5);
editText.setText(CommentFromDB);
editText.setEnabled(false);
tableLayout.addView(editText);
// Add table in Horizontal scroll view
scrollView.addView(tableLayout);
现在我想使编辑文本可滚动,我通过代码实现:
editText.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
if (view.getId() == 1) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction()&MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
case MotionEvent.ACTION_DOWN:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
但是edittext不容易滚动。我需要平滑滚动。
如何做到这一点请指导我。
答案 0 :(得分:7)
你可以做一件事。
只需将编辑文本设为焦点错误。并适用于触控听众。
因此用户无法编辑文本,它将滚动为:
EditText editText = new EditText(this);
editText.setId(1);
editText.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,0f));
editText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
editText.setGravity(Gravity.TOP|Gravity.LEFT);
editText.setHint("Comment");
editText.setSingleLine(false);
editText.setLines(5);
editText.setMaxLines(5);
editText.setText(CommentFromDB);
editTextRemark.setFocusable(false);
将onTouchListner应用为:
editTextRemark.setOnTouchListener(touchListener);
和
OnTouchListener touchListener = new View.OnTouchListener(){
public boolean onTouch(final View v, final MotionEvent motionEvent){
if(v.getId() == 1){
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
};
希望这个答案能帮到你。
答案 1 :(得分:6)
EditText editText = findViewById(R.id.editText);
editText.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
if (view.getId() ==R.id.common_remark) {
view.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction()&MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_UP:
view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
在xml中,在EditText中添加以下行:
android:scrollbars = "vertical"
答案 2 :(得分:0)
添加到EditText
android:inputType="textMultiLine"
答案 3 :(得分:0)
在 kotlin 中使用这个
editText.setOnTouchListener { v, event ->
v.parent.requestDisallowInterceptTouchEvent(true)
when (event.action and MotionEvent.ACTION_MASK) {
MotionEvent.ACTION_UP -> v.parent.requestDisallowInterceptTouchEvent(false)
}
false
}