大家!我是Android开发的新手,在尝试为搜索栏的拇指设置特定位置时遇到了麻烦。
我想在用户释放拇指时将搜索栏的进度设置为它的初始位置(在本例中为4),为此,我尝试使用onStopTrackingTouch方法,但是看起来在何时未调用它用户释放搜寻栏拇指。 Eclipse没有显示任何错误,程序正常运行,唯一的问题是,当我释放拇指时,它不会返回到初始位置。
详细信息:我正在使用垂直搜索栏,这可能是问题的原因吗?
这是 Activity 代码:
package renatox.play.bluetoothcommander;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SeekBar;
public class MainActivity extends Activity implements SensorEventListener {
private VerticalSeekBar acelerador;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Sets the Main Activity layout
setContentView(R.layout.activity_main);
// Gets the VerticalSeekBar
acelerador = (VerticalSeekBar) findViewById(R.id.acelerador);
acelerador
.setOnSeekBarChangeListener(new VerticalSeekBar.OnSeekBarChangeListener() {
// I've tried using SeekBar.OnSeekBarChangeListener() and VerticalSeekBar.OnSeekBarChangeListener()
@Override
public void onStopTrackingTouch(SeekBar arg0) {
acelerador.setProgress(4);
Log.i("Acelerador", "O usuário soltou o acelerador");
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
Log.i("Acelerador", "O usuário pegou o acelerador");
}
// Pega os valores do acelerador quando mudam
@Override
public void onProgressChanged(SeekBar arg0, int arg1,
boolean arg2) {
int velocidade = acelerador.getProgress();
Log.i("Acelerador", "Velocidade atual: " + velocidade);
}
});
}
这是xml布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fundo"
android:keepScreenOn="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<renatox.play.bluetoothcommander.VerticalSeekBar
android:id="@+id/acelerador"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_marginBottom="30dp"
android:layout_marginRight="5dp"
android:layout_marginTop="30dp"
android:max="8"
android:maxHeight="0dp"
android:minHeight="0dp"
android:progress="4"
android:progressDrawable="@drawable/progress"
android:thumb="@drawable/thumb" />
</RelativeLayout>
这是VerticalSeekBar类
package renatox.play.bluetoothcommander;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.SeekBar;
public class VerticalSeekBar extends SeekBar {
protected OnSeekBarChangeListener changeListener;
public VerticalSeekBar(Context context) {
super(context);
}
public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public VerticalSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(h, w, oldh, oldw);
}
@Override
protected synchronized void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
super.onMeasure(heightMeasureSpec, widthMeasureSpec);
setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}
protected void onDraw(Canvas c) {
c.rotate(-90);
c.translate(-getHeight(), 0);
super.onDraw(c);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
setSelected(true);
setPressed(true);
if (changeListener != null)
changeListener.onStartTrackingTouch(this);
break;
case MotionEvent.ACTION_UP:
setSelected(false);
setPressed(false);
if (changeListener != null)
changeListener.onStopTrackingTouch(this);
break;
case MotionEvent.ACTION_MOVE:
int progress = getMax()
- (int) (getMax() * event.getY() / getHeight());
setProgress(progress);
onSizeChanged(getWidth(), getHeight(), 0, 0);
if (changeListener != null)
changeListener.onProgressChanged(this, progress, true);
break;
case MotionEvent.ACTION_CANCEL:
break;
}
return true;
}
}
谁能给我提示可能发生的事情吗?
非常感谢你!!
最佳答案
我以为我会将其发布为正式的“答案”,因为它可以帮助我编写代码。 (由Renato Henz回答)
我刚刚将其添加到“VerticalSeekBar.java”中:@Override public void setOnSeekBarChangeListener(OnSeekBarChangeListener mListener) { this.changeListener = mListener; }
如果您需要其他帮助,请告诉我。祝您的代码好运!
我发现添加此代码以及前面提到的代码可以解决一些问题:
@Override
public synchronized void setProgress(int progress) {
super.setProgress(progress);
onSizeChanged(getWidth(), getHeight(), 0, 0);
}
关于java - Android-VerticalSeekBar-onStopTrackingTouch()无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21687423/