如何在EditText中自动滚动?
scrollTo(x,y)不起作用... = /
final EditText edittext1 = (EditText) findViewById(R.id.editText1);
final EditText edittext2 = (EditText) findViewById(R.id.editText2);
//edittext1.setKeyListener(null);
edittext2.requestFocus();
edittext2.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
getWindow()
.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
edittext1.setText(edittext1.getText() + "\n"
+ edittext2.getText() + "\n\n");
funcoes(edittext2.getText().toString());
edittext1.scrollTo(0, edittext1.getLayout().getLineCount());
edittext2.setText("");
return true;
}
return false;
}
});
我在ScrollView中尝试过colocoar,但效果不佳。 有没有办法在EditText中滚动?
感谢
答案 0 :(得分:1)
尝试替换它:
edittext1.scrollTo(0, edittext1.getLayout().getLineCount());
用这个:
int y = (edittext1.getLineCount() - 1) * edittext1.getLineHeight(); // the " - 1" should send it to the TOP of the last line, instead of the bottom of the last line
edittext1.scrollTo(0, y);
因为scrollTo(x,y)
以像素为单位测量x和y,并且您向它发送了行数。 getLineHeight
返回每行的像素数,所以
lineHeight * lineCount =总像素高度。
(另外,我猜你想要edittext1的LineCount,而不是它的父布局。)