【发布时间】: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">< 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);
然后在调试器中设置断点确认按钮始终处于触摸模式(无论我是向上/向下拖动面板,还是向上/向下滚动面板)。因此,在第二次按下按钮后(即,当应用程序处于“停止/暂停计时”模式时)处于触摸模式和手指拖动面板的组合会导致后续暂停中出现奇怪的额外计时。
更新 2: 我只是注意到它正在向下滚动到 EditText,没有进一步了。看起来当您将面板向下移动时,EditText 获得了选择,并且在单击事件之后,ScrollView 滚动回具有选择的事物。似乎可以解释为什么鼠标滚轮方法没有这个问题(它将选择/焦点移回按钮)。
【问题讨论】:
标签: android