【问题标题】:Why does TextView.setText cause the enclosing ScrollView to scroll?为什么 TextView.setText 会导致封闭的 ScrollView 滚动?
【发布时间】:2010-06-24 20:01:57
【问题描述】:

我在 1.6、2.2 和 MyTouch 3G 幻灯片(API #7,并在 Android 设备选择器中列为“2.1-Update1”)上遇到了这个奇怪的问题。如果有人能解释我做错了什么以及如何修复它(或者可能确认这是一个 Android 错误),我将不胜感激!

我的应用程序的基本理念是做一个秒表类的东西,用户可以点击一个按钮来启动计时器,然后再次点击它来停止(暂停)计时器;更多的点击在恢复计时器和暂停计时器之间交替。

我有一个顶级 ScrollView,其中包含一个 RelativelLayout,其中包含一堆小部件。第一个小部件是一个巨大的按钮(因此很容易按下),它将我所有其他小部件推到屏幕底部下方。这是故意的,因为我想依靠 ScrollView(以及屏幕上的用户提醒)来使其余的输入选项可用。

我有一个简单的状态机类型设置,其中 mState 是当前模式(用户按下任何按钮之前的 STATE_TIMER_NOT_STARTED,第一次按下后 ...RUNNING,第二次按下后 ...PAUSED,返回到...在第三个之后运行,等等)。

所有这一切都很好,除了当计时器运行时,用户再次按下开始/停止/恢复按钮,ScrollView 将向下滚动。我没有发出这个命令(我什至没有对 ScrollView 对象的引用),我不确定它为什么这样做。

复制: 编译 + 运行以下示例。当应用程序启动时,按“开始计时”按钮。使用拇指(或鼠标)向上触摸拖动屏幕(以便您可以看到 RatingBar),然后将其向下拖动(使按钮再次完全显示在屏幕上)。再次点击按钮(现在显示为“PauseTiming”),它会向下跳一点。它不应该跳跃/向下滚动,因为没有语句(我可以看到)告诉它向下滚动。据我所知,是 setText 导致滚动(当我将这些行注释掉时,不会发生滚动)。

我的要求: 如果我在做一些愚蠢的事情,你可以指出它是什么,我真的很感激! :) *** 我想知道“触摸模式”是否与此有关,因为当我使用鼠标的滚轮向上移动面板时(即,而不是模拟手指),它似乎不会发生(在模拟器中) -拖动)。我在触摸模式上找不到很多内容,也没有关于 ScrollView 中触摸模式下的焦点/选择的具体内容

如果你能确认这个错误也发生在你身上,那也没关系(因为苦难爱陪伴。AHEM我的意思是,因为它可能有助于确认它不仅仅是我 :) )。

MyTestApp.java

package bug.android.scrollview;

import android.app.Activity;
import android.os.Bundle;
import android.text.format.Time;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class MyTestApp extends Activity {

    public final static int STATE_TIMER_NOT_STARTED = 1;
    public final static int STATE_TIMER_RUNNING = 2;
    public final static int STATE_TIMER_PAUSED = 3;
    private int mState;

    Time t = new Time();

    private Time data = new Time();

    private Button btnStartStopResume;
    private TextView lblSpacer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_time_entry);

        btnStartStopResume = (Button) findViewById(R.id.btnStartStopResume);

        // Set the button's size so that the other info will also be visible
        Display display = ((WindowManager) getSystemService(WINDOW_SERVICE))
                .getDefaultDisplay();
        // This is such a hack, but the windowScroller doesn't appear to
        // have a height at this point in the lifecycle (nor in 'onResume' :( )
        btnStartStopResume.setHeight(display.getHeight() - 200);

        lblSpacer = (TextView) findViewById(R.id.lblSpacer);

        reset();
    }

    public void doStartStopResume(View v) {
        if (mState == MyTestApp.STATE_TIMER_NOT_STARTED) {

            mState = MyTestApp.STATE_TIMER_RUNNING;

            data.setToNow();

        } else if (mState == MyTestApp.STATE_TIMER_RUNNING) {
            mState = MyTestApp.STATE_TIMER_PAUSED;

            String s = getString(R.string.add_scroll_down_to_add);
            lblSpacer.setText(s);
        } else if (mState == MyTestApp.STATE_TIMER_PAUSED) {

            mState = MyTestApp.STATE_TIMER_RUNNING;
        }
    }
    public void doReset(View v) {
    }

    public void doNewRunClick(View v) {
    }

    public void doAddTiming(View v) {
    }

    public void reset() {
        mState = STATE_TIMER_NOT_STARTED;
    }
}

new_time_entry.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/windowScroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
>
    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    >
        <Button
            android:id="@+id/btnStartStopResume"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dip"
            android:text="Start Timing"
            android:textSize="40dp"
            android:height="290dp"
            android:onClick="doStartStopResume" />

        <TextView
            android:id="@+id/lblSpacer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/btnStartStopResume"
            android:layout_centerHorizontal="true"
            android:text="@string/add_scroll_down_for_more" />

        <TextView
            android:id="@+id/lblTimeStartLabel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/lblSpacer"
            android:layout_alignParentLeft="true"
            android:clickable="true"
            android:onClick="adjustStartTime"
            android:text="Start of this run:"
            android:textSize="8dp" />

        <TextView
            android:id="@+id/lblTimeStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/lblTimeStartLabel"
            android:layout_alignParentLeft="true"
            android:clickable="true"
            android:onClick="adjustStartTime"
            android:text="--:--:-- --"
            android:textColor="#FFFFFF"
            android:textSize="26dp" />

        <TextView
            android:id="@+id/lblElapsedLabel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/lblSpacer"
            android:layout_alignRight="@id/lblSpacer"
            android:layout_marginRight="5dp"
            android:text="Elapsed Time:"
            android:textSize="8dp" />

        <TextView
            android:id="@+id/lblTimeElapsed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/lblElapsedLabel"
            android:layout_alignRight="@id/lblSpacer"
            android:layout_marginRight="5dp"
            android:textColor="#99ff66"
            android:text="-- m -- sec"
            android:textSize="26dp" 
            android:layout_marginBottom="10dip"/>

        <CheckBox
            android:id="@+id/chkNewRun"
            android:onClick="doNewRunClick"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/lblTimeElapsed"
            android:text="This is a new run of timings"
            android:layout_marginBottom="10dip" />

        <TextView
            android:id="@+id/lblIntensity"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Intensity (1 = none 5 = max)"
            android:layout_below="@id/chkNewRun" />

        <RatingBar
            android:id="@+id/rbIntensity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/lblIntensity"
            android:numStars="5"
            android:rating="2"
            android:layout_marginBottom="5dip" />

        <TextView
            android:id="@+id/lblNotes"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Notes:"
            android:layout_below="@id/rbIntensity" />

        <EditText
            android:id="@+id/txtNotes"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/editbox_background"
            android:layout_below="@id/lblNotes"
            android:layout_marginBottom="10dip" />


        <Button
            android:id="@+id/btnReset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/txtNotes"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:text="Reset"
            android:onClick="doReset" />

        <Button
            android:id="@+id/btnOk"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/txtNotes"
            android:layout_toRightOf="@id/btnReset"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:text="Add Timing To List"
            android:onClick="doAddTiming" />
    </RelativeLayout>
</ScrollView>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Timer</string>
    <string name="dlg_edit_timing_title">Edit A Timing</string>
    <string name="add_scroll_down_for_more">&lt; Scroll down for more options! &gt;</string>
    <string name="add_scroll_down_to_add">&lt; Scroll down to save this timing! &gt;</string>
    <string name="start_timing">Start Timing\n\n</string>
    <string name="stop_timing">Pause Timing\n\n</string>
    <string name="resume_timing">Resume Timing\n\n</string>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="bug.android.scrollview"
    android:versionCode="1"
    android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".MyTestApp"
        android:label="@string/app_name">
        <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="5" />
</manifest>

更新1:添加

        if(  btnStartStopResume.isInTouchMode() )
            Toast.makeText(this, "TOUCH MODE", 2000);
        else
            Toast.makeText(this, "NOT touch mode", 2000);

然后在调试器中设置断点确认按钮始终处于触摸模式(无论我是向上/向下拖动面板,还是向上/向下滚动面板)。因此,在第二次按下按钮后(即,当应用程序处于“停止/暂停计时”模式时)处于触摸模式和手指拖动面板的组合会导致后续暂停中出现奇怪的额外计时。

更新 2: 我只是注意到它正在向下滚动到 EditText,没有进一步了。看起来当您将面板向下移动时,EditText 获得了选择,并且在单击事件之后,ScrollView 滚动回具有选择的事物。似乎可以解释为什么鼠标滚轮方法没有这个问题(它将选择/焦点移回按钮)。

【问题讨论】:

标签: android


【解决方案1】:

旧线程,但我想我会添加这个,以防有人像我一样四处寻找。我有同样的问题,但只是清除焦点并没有帮助。这就是最终为我解决的问题:

editText.clearFocus();
editText.setTextKeepState(text);

希望这对某人有所帮助。 TextView docs

【讨论】:

    【解决方案2】:

    好的,我已经掌握了正在发生的事情,并且对如何解决这个问题有了足够的了解,我想我不妨在这里发布这个作为答案:

    如果您在调用 setText 之前在 EditText 上调用 clearFocus(这既是按钮事件处理程序,也是我通过该程序的“真实”版本中的线程处理程序运行的几个计时器),那么一切以我期望的方式工作(没有奇怪的自动滚动)。

    如果您想使用此解决方案,您需要清除任何可能获得焦点的焦点,但这使得它成为一个蹩脚的解决方案 - 我会尝试继续关注这个。在 ScrollView 文档下,有一个名为 onRequestFocusInDescendants 的方法,带有神秘的注释“在滚动视图的子视图中寻找焦点时,需要更加小心,不要将焦点放在滚动到屏幕外的东西上。这比默认 ViewGroup 实现,否则此行为可能已成为默认行为。”这可能表明这里发生了什么......

    【讨论】:

    • 谢谢,我刚刚遇到了这个问题,并通过清除焦点解决了这个问题,这在我的情况下还可以——感谢您发布您自己问题的答案。
    【解决方案3】:

    我在此页面上尝试了解决方案,但它们对我不起作用。每次单击视图中的刷新按钮时,我都有一个滚动视图。我最终做的是使按钮左侧的标题可聚焦,并将该项目设置为在我的按钮回调中聚焦。

    我的片段中的 onCreateView 的一部分:

    final View view = inflater.inflate(R.layout.fragment_diagnostic, container, false);
    view.findViewById(R.id.button_refresh_bluetooth_device).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            updateBluetoothDevice();
            view.findViewById(R.id.bluetooth_device_title).requestFocus();
        }
    });
    view.findViewById(R.id.button_refresh_network).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            updateNetworkSection();
            view.findViewById(R.id.android_network_title).requestFocus();
        }
    });
    

    我的布局的简化版:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/parentPanel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/alert_dialog_padding_material"
        android:orientation="vertical">
    
        <RelativeLayout
            android:id="@+id/topPanel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:paddingLeft="@dimen/alert_dialog_padding_material"
            android:paddingRight="@dimen/alert_dialog_padding_material"
            android:paddingBottom="@dimen/floating_action_button_margin">
    
            <android.support.design.widget.FloatingActionButton
                android:id="@+id/button_refresh"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:layout_alignParentRight="true"
                android:padding="@dimen/floating_action_button_margin"
                android:src="@drawable/ic_fab_refresh"
                android:contentDescription="@string/content_description_refresh"
                app:elevation="4dp"
                app:borderWidth="0dp"
                app:fabSize="mini"/>
    
            <TextView
                android:id="@+id/alertTitle"
                style="?android:attr/windowTitleStyle"
                android:singleLine="true"
                android:ellipsize="end"
                android:layout_toLeftOf="@id/button_refresh"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:paddingBottom="@dimen/floating_action_button_margin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/title_diagnostics"/>
        </RelativeLayout>
    
        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:clipChildren="false"
            android:clipToPadding="false"
            android:layout_weight="1"
            android:orientation="vertical"
            android:minHeight="48dp"
            android:paddingLeft="@dimen/alert_dialog_padding_material"
            android:paddingRight="@dimen/alert_dialog_padding_material"
            android:paddingBottom="@dimen/alert_dialog_padding_material">
    
            <TableLayout
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:clipChildren="false"
                android:shrinkColumns="1"
                android:stretchColumns="1,2">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical">
    
                    <TextView
                        android:id="@+id/bluetooth_device_title"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:text="@string/title_bluetooth_device"
                        android:focusable="true"
                        android:focusableInTouchMode="true"/>
    
                    <android.support.design.widget.FloatingActionButton
                        android:id="@+id/button_refresh_bluetooth_device"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="@dimen/floating_action_button_margin"
                        android:src="@drawable/ic_fab_refresh"
                        android:contentDescription="@string/content_description_refresh"
                        app:elevation="4dp"
                        app:borderWidth="0dp"
                        app:fabSize="mini"/>
                </LinearLayout>
    
                <TableRow>
    
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="5dp"
                        android:layout_gravity="center_vertical"
                        android:id="@+id/bt_connection_status_image"
                        tools:src="@drawable/ic_check"
                        android:contentDescription="@string/content_description_diagnostic_status"/>
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_gravity="center_vertical"
                        android:text="@string/label_state"/>
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_gravity="center_vertical"
                        tools:text="Connected"
                        android:id="@+id/bt_connection_status"/>
    
                </TableRow>
    
                <LinearLayout
                    android:id="@+id/android_network_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical">
    
                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:text="@string/title_android_to_network"
                        android:focusable="true"
                        android:focusableInTouchMode="true"/>
    
                    <android.support.design.widget.FloatingActionButton
                        android:id="@+id/button_refresh_network"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="@dimen/floating_action_button_margin"
                        android:src="@drawable/ic_fab_refresh"
                        android:contentDescription="@string/content_description_refresh"
                        app:elevation="4dp"
                        app:borderWidth="0dp"
                        app:fabSize="mini"/>
                </LinearLayout>
                <TableRow>
    
                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="5dp"
                        android:layout_gravity="center_vertical"
                        android:id="@+id/android_network_state_image"
                        tools:src="@drawable/ic_check"
                        android:contentDescription="@string/content_description_diagnostic_status"/>
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_gravity="center_vertical"
                        android:text="@string/label_state"/>
    
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_gravity="center_vertical"
                        tools:text="Connected"
                        android:id="@+id/android_network_state"/>
    
                </TableRow>
            </TableLayout>
        </ScrollView>
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-10
      • 2021-07-11
      • 2019-09-14
      • 2013-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多