java - 使用不同的 Button 更改 SeekBar 的 slider 位置

标签 java android seekbar

我正在尝试使用按钮移动搜索栏的位置。基本上我有一个从 0 到 100 的搜索栏。并且我有设置为任意值(40、50、60 等)的按钮呈现。当我尝试通过按钮设置搜索栏上的进度时,出现错误。我已经在 onCreate() 方法中初始化了搜索栏。

    SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar1);
    currentProgress = 40;
    seekBar.setMax(100);
    seekBar.setProgress(currentProgress);
    button40.setOnClickListener(button40Listener);

但是当使用下面的时候,它崩溃了。

    private OnClickListener button40Listener = new OnClickListener() {
        public void onClick(View v) {
          currentProgress = 40;
          seekBar.setProgress(currentProgress);
        }
    }

这看起来很简单。有任何想法吗?

最佳答案

您不需要放置另一个搜索栏。第一个应该没问题。如果没有异常消息和堆栈跟踪,我不确定导致崩溃的原因。不过,我只是编写了一个示例,并且按照您的预期工作。也许通过查看我的示例,您可以确定您的问题。

SeekbarTest.java:

package com.example.seekbartest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;

public class SeekbarTest extends Activity {
/** Called when the activity is first created. */
private SeekBar seekBar;

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

    seekBar = (SeekBar)findViewById(R.id.seekBar1);

    Button fortyPctButton = (Button)findViewById(R.id.buttonFortyPct);
    fortyPctButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            seekBar.setProgress(40);
        }
    });

    Button sixtyPctButton = (Button)findViewById(R.id.buttonSixtyPct);
    sixtyPctButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            seekBar.setProgress(60);
        }
    });

    Button eightyPctButton = (Button)findViewById(R.id.buttonEightyPct);
    eightyPctButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            seekBar.setProgress(80);
        }
    });
    }
}

这是布局引用的 main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView 
    android:layout_width="fill_parent" 
    android:text="@string/hello"
    android:id="@+id/textView1" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true"/>

<SeekBar 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/seekBar1" 
    android:layout_below="@+id/textView1" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_alignRight="@+id/textView1"/>

<Button 
    android:layout_width="wrap_content" 
    android:text="40%" 
    android:id="@+id/buttonFortyPct" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/seekBar1"
    android:layout_alignLeft="@+id/seekBar1"/>

<Button 
    android:layout_width="wrap_content" 
    android:text="60%" 
    android:id="@+id/buttonSixtyPct" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/buttonFortyPct" 
    android:layout_alignTop="@+id/buttonFortyPct" 
    android:layout_alignBottom="@+id/buttonFortyPct"/>

<Button 
    android:layout_width="wrap_content" 
    android:text="80%" 
    android:id="@+id/buttonEightyPct" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@+id/buttonSixtyPct" 
    android:layout_alignTop="@+id/buttonSixtyPct" 
    android:layout_alignBottom="@+id/buttonSixtyPct"/>
</RelativeLayout>

只需创建一个新的 Android 应用程序,并将生成的代码 + 布局替换为上面的示例。它应该适合你。

祝你好运, 克雷格

关于java - 使用不同的 Button 更改 SeekBar 的 slider 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6222302/

相关文章:

android - 更改签名 key ,保留包名

android - 向音频播放器添加搜索栏后,为什么经常打开和关闭声音?

android - 如何在搜索栏中设置双值

java - 如何在给定的不活动时间后隐藏 View

java - 如何使用java中的正则表达式用字符串中的连续数字替换某些子字符串?

java - 区域设置和多语言支持的最佳实践是什么?

java - GridBagLayout 交错按钮

java - 在 2D JButton 阵列上添加图像

java - 如何为不实现 parcelable 的对象 Arraylist 实现 Parcelable?

android - 如何以编程方式在 AsyncTask.onPreExecute 中添加 ProgressBar?