Auto scroll down on textviews

蓝咒 提交于 2019-12-11 16:53:09

问题


I have 3 components on my application, 1 textview(inputType:textMultiline, scrollbar:vertical, gravity:bottom|right) at the top, 1 editview at the middle and 1 button at the bottom. When I type something on my editview and I click the button, the text written on the edit view displays on the textview. Every time i hit ok, the text displays the the bottom and what is displayed is the first three inputs. I have to scroll down the textview in able for me to see my last input.

Now, I want my users to see their last input on their textview. I want to know if there is such code for auto scrolldown for textviews everytime I input a new text on it. I am new on developing android apps. Thanks!


回答1:


When you set text to textview just set foucus to it. like

tv.setFocusable(true);

It will automatically focus your view whenever you change your string on textview.

If you are adding text to your text view again and again then you can try this

    int scroll_amount = tv.getBottom();
    tv.scrollTo(0, scroll_amount);

Hope it will work not sure..

Try this also

    int scroll_amount = (int) (tv.getLineCount() * tv.getLineHeight()) - (tv.getBottom() - tv.getTop());
    tv.scrollTo(0, scroll_amount);



回答2:


you can use scrollView.scrollTo(x,y) to auto scroll to position that you want.

/*edit*/

create custom class for scrollview

package com.android.mypackage
public class myScrollView extends ScrollView{
    private int maxY = 0;
    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
       if(y>maxY)
        maxY=y;
    }
    public void moveToEnd(){
        this.scrollTo(0, maxY);
    }
}

using this custom class in layout xml as below:

...
<com.android.mypackage.myScrollView
    android:id="@+id/my_scrollview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView .../>
</com.android.mypackage.myScrollView>
.....

when you press Ok button just call function myscrollViewObj.movetoEnd(); it's just a draft code, still not tested.




回答3:


I already got the answer for this! Thank you for giving me some idea. I might be able to use them in the future. @Bharat Sharma, you almost got the answer! Thanks!

public void onClick(View v) {
            // TODO Auto-generated method stub
            log.setText(log.getText() + "\n" +input.getText());

            if(log.getLineCount() > 5){
                scroll_amount = scroll_amount + log.getLineHeight();
                log.scrollTo(0, scroll_amount);
            }
        }

I called the variable scroll amount outside the onCreate(). Thanks again!




回答4:


This is it.

public void onScrollDownClicked(View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        mTextView.scrollTo(0, mTextView.getLayout().getHeight() - mTextView.getHeight());
    }
}


来源:https://stackoverflow.com/questions/10326397/auto-scroll-down-on-textviews

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!