gpt4 book ai didi

java - Android - VerticalSeekBar - onStopTrackingTouch() 不工作

转载 作者:太空宇宙 更新时间:2023-11-03 13:25:07 24 4
gpt4 key购买 nike

各位!我是 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 回答)

I've just added this to the "VerticalSeekBar.java": @Override public void setOnSeekBarChangeListener(OnSeekBarChangeListener mListener) { this.changeListener = mListener; } If you need any further help, please tell me. Good luck with your code!

我发现添加这段代码以及前面提到的代码有助于解决一些问题:

 @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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com