我有这个奇怪的问题,发生在1.6,2.2和MyTouch 3G幻灯片(这是API#7,在Android设备选择器中列为“2.1-Update1”)。如果有人能解释我做错了什么&如何修复它(或者可能确认这是一个Android错误),我非常感谢!
我的应用程序的基本思想是制作秒表式的东西,因为用户可以点击按钮启动计时器,然后再次点击它以停止(暂停)计时器;进一步的点击在恢复计时器和暂停计时器之间交替。
我有一个包含RelativelLayout的顶级ScrollView,它包含一堆小部件。第一个小部件是一个巨大的按钮(这样很容易按下),它将我的所有其他小部件推到屏幕底部。这是故意的,因为我想依靠ScrollView(以及对用户的屏幕提醒)来使其余的输入选项可用。
我有一个简单的状态机类型设置,其中mState是当前模式(在用户按下任何按钮之前STATE_TIMER_NOT_STARTED,...在第一次按下后运行,然后......在第二次按下之后暂停,返回到...在第三次之后运行等等)。
所有这一切都很有效除了当计时器正在运行,并且用户再次按下开始/停止/恢复按钮时,ScrollView将向下滚动一些方式。我没有发出这个命令(我甚至没有ScrollView对象的引用),我不确定它为什么这样做。
REPRO: 编译+运行以下示例。当应用程序启动时,按“开始计时”按钮。用拇指(或鼠标)向上触摸屏幕(这样你就可以看到RatingBar),然后向下拖动它(这样按钮再次完全在屏幕上)。点击按钮(现在再次显示'PauseTiming'),它会跳了一下。它不应该跳跃/向下滚动,因为没有声明(我可以看到)告诉它向下滚动。就像我所知,它是导致滚动的setText(当我将这些行注释掉时,不会发生滚动)。
我要求的是: 如果我正在做一些愚蠢的事情。你可以指出它是什么,我真的很感激! :) ***我想知道'触摸模式'是否可能与此有关,因为当我使用鼠标的滚轮向上移动面板(即,而不是模拟的手指)时,它似乎不会发生(在模拟器中) -dragging)。我在触摸模式上找不到很多东西,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">< Scroll down for more options! ></string>
<string name="add_scroll_down_to_add">< Scroll down to save this timing! ></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);
然后在调试器中设置断点确认按钮始终处于触摸模式(无论我是向上/向下手指拖动面板,还是向上/向下鼠标滚动)。因此,它是触摸模式和按下第二个按钮后手指拖动面板的组合(即,当应用程序处于“停止/暂停计时”模式时),这导致后续暂停中的奇数额外时间。 / p>
更新2: 我刚刚注意到它正在向下滚动到EditText,而没有进一步。看起来当您向下移动面板时,EditText会获得选择,并且在单击事件之后,ScrollView会滚动回到具有所选内容的事物。似乎解释了为什么鼠标滚轮方法没有这个问题(它将选择/焦点移回到按钮)。
答案 0 :(得分:5)
旧线程,但我想我会添加这个以防万一有人像我一样搜索。我有同样的问题,但只是清除焦点没有帮助。这就是最终为我解决的问题:
editText.clearFocus();
editText.setTextKeepState(text);
希望这有助于某人。 TextView docs
答案 1 :(得分:2)
好的,我已经掌握了正在发生的事情,并且已经足够了解如何解决这个问题,我认为我可能会在这里发布,作为答案:
如果你在调用setText之前在EditText上调用clearFocus(它既是按钮事件处理程序,也是我在这个程序的'真实'版本中通过线程的Handler运行的几个计时器),那么一切按照我的预期工作(没有奇怪的自动滚动)。
如果你想使用这个解决方案,你需要清除焦点,因为任何可能引起关注的事情,这使得它成为一个蹩脚的解决方案 - 我会试着继续关注这个问题。在ScrollView文档下,有一个名为onRequestFocusInDescendants的方法,其中含有神秘的音符“当在滚动视图的子节点中寻找焦点时,需要更加小心,不要将焦点放在屏幕滚动的内容上。这比默认的ViewGroup实现,否则这个行为可能是默认的。“这可能会指出这里发生了什么......
答案 2 :(得分:0)
我在这个页面上尝试过解决方案但它们对我不起作用。每次单击视图中的刷新按钮时,我都会滚动滚动视图。我最终做的是制作一个位于按钮左侧的标题,并将该项目设置为我的按钮回调。
我的片段中的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>