Android:使多行edittext可滚动,在垂直滚动视图中禁用 [英] Android: Make multiline edittext scrollable, disable in vertical scroll view
问题描述
我正在开发一个可以在其中构建结构的应用程序.
I am developing an application in which i am struct at a point.
根据我的应用程序要求,我在xml中创建了水平滚动视图,然后在.java中创建了垂直滚动视图:
As according to my application requirement i created horizontal scrollview in xml and then vertical scrollview in .java as :
// Vertical Scroll view in Linear layout
ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
然后我以编程方式创建了一个表格视图,并将其添加到scrollview中.我创建了多行编辑文本并将其禁用,因为我想在运行时设置文本并将其添加到表视图中作为一行..
Then i created a table view programatically and added it in scrollview. I created multiline edit text and disable it because i want to set text in it run time and added this in table view as a row..
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);
现在我想使编辑文本可滚动,这是我通过代码实现的:
Now i want to make edit text scrollable which i achieve by code:
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并不容易滚动.我需要平滑滚动.
But the edittext is not scrolling easily. I need the smooth scrolling.
该怎么做,请指导我.
推荐答案
您可以做一件事.
只需将编辑文本的焦点设置为false.并应用到触摸监听器上.
Just make edit text focusable false. And apply on touch listener.
因此用户无法编辑文本,它将滚动为:
So user is not able to edit text and it will scroll as:
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应用为:
Apply onTouchListner as:
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;
}
};
希望这个答案对您有帮助.
Hope this answer will help you.
这篇关于Android:使多行edittext可滚动,在垂直滚动视图中禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!