使用不同的按钮更改SeekBar的拇指位置

时间:2011-06-03 02:05:47

标签: java android seekbar

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

    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);
        }
    }

这似乎是直截了当的。有任何想法吗?

1 个答案:

答案 0 :(得分:3)

你不应该提出另一个Seekbar。最初的应该没问题。没有异常消息和堆栈跟踪,我不确定导致崩溃的原因。但是,我只编写了一个示例并按预期工作。也许通过查看我的示例,您可以确定您的问题。

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应用程序,并使用上面的示例替换生成的代码+布局。它应该适合你。

祝你好运, 克雷格