Android - 在 AlertDialog TextView 中自动滚动

标签 android scroll textview android-alertdialog

我正在使用 AlertDialog 来跟踪实时更新的日志文件,并且需要在添加额外行时自动滚动到 View 底部。

我能够将 AlertDialog 转换为 TextView(例如,使用此 TextView 改变文本大小)但是任何涉及滚动的方法都不起作用。

代码:

LogFileView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                try {
                    String logFile = "/data/data/com.test/test.log";
                    String logFileOutput = getFileOutput(logFile);
                    final AlertDialog dialog = new AlertDialog.Builder(getActivity()).setMessage(logFileOutput).show();
                    TextView textView = dialog.findViewById(android.R.id.message);
                    textView.setTextSize(8);
                    textView.scrollTo(0, textView.getLineCount());

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }); 

textView.setTextSize(8); 将改变显示的文本大小

textView.scrollTo(0, textView.getLineCount()); 什么都不做,尽管有可用的滚动条,警告对话框仍将集中在第一行

更新 1:

我看到控制台输出中有一些对话框创建代码/错误请求。

首先,我实际上并没有使用单独的布局/类来创建对话框。它将与 (android.R.id.message) 关联的默认布局应用于 android.app.AlertDialog 的实例,并且仅在 onClick 方法中构造上面代码中的 onClickListener。

根据我目前收到的反馈,我最近尝试使用的代码如下所示:

LogFileView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            try {
                String logFile = "/data/data/com.test/test.log";
                String logFileOutput = getFileOutput(logFile);
                final AlertDialog dialog = new AlertDialog.Builder(getActivity()).setMessage(logFileOutput).show();
                TextView textView = dialog.findViewById(android.R.id.message);
                textView.setTextSize(8);
                //textView.scrollTo(0, textView.getLineCount());
                textView.setMovementMethod(new ScrollingMovementMethod());
                textView.post(new Runnable() {
                    @Override
                    public void run() {
                        textView.scrollTo(0, textView.getLineCount());
                    }
                });

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

其次,当进行滚动尝试时,控制台中不会出现任何内容 - 这在运行时会被忽略。

默认布局似乎适合我的目的,因为它看起来像一个带有滚动条的空白 TextView,但我认为下一步使用新的自定义布局并添加 ScrollView 并查看可能是明智的发生了什么

最佳答案

使用下面的代码。我测试并验证了。

    textView.movementMethod = ScrollingMovementMethod() // Add this
    textView.text = "your text"
    textView.post {
        val scrollAmount = textView.layout.getLineTop(textView.lineCount) - textView.height
        textView.scrollTo(0, textView.lineCount)
    }

编辑: 相当于java:

    textView.setMovementMethod(new ScrollingMovementMethod());
    textView.setText("your text");
    textView.post(new Runnable() {
        @Override
        public void run() {
            int scrollAmount = textView.getLayout().getLineTop(textView.getLineCount()) - textView.getHeight();
            textView.scrollTo(0, scrollAmount);
        }
    });

关于Android - 在 AlertDialog TextView 中自动滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54211139/

相关文章:

android - 用于滑过操作栏的自定义抽屉布局

Android:如何在 Tablerow 中将三个 Textview 居中?

java - 如何在 Android 中以自定义布局显示 HTML 页面内容

android - 通过 BLE 的音乐播放器

安卓用户界面 : TransitionDrawable

android - Ionic - 更新启动画面不起作用

android - 如果 fragment 包含 AppBarLayout,BottomNavigationView 不会隐藏在 fragment 内滚动

ios - 带有 NSMutableArray 的表的奇怪行为

javascript - 在尝试再次执行之前等待函数完成执行的问题

android - 在 TextView 中创建超链接