更改使用不同的按钮的搜索栏的拇指位置 [英] Changing the position of the thumb of the SeekBar using a different Button

查看:222
本文介绍了更改使用不同的按钮的搜索栏的拇指位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想移动使用按钮搜索栏的位置。基本上我有从0到100的搜索栏,我有按钮presents在任意值(40,50,60等)成立。当我尝试设置通过按钮搜索栏的进展,我得到一个错误。我已经初始化在OnCreate()搜索栏的方法。

I'm trying to move the position of the seekbar using a button. Basically I have a seekbar from 0 to 100. and I have button presents set up at arbitrary values (40,50,60 etc). When I try to set the progress on the seekbar via button, I get a fault.. I've already initialized the seekBar in the onCreate() method.

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

但使用以下时,崩溃。

But when use the below, it crashes.

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

这似乎是直线前进。有任何想法吗?

This seems straight-forward. Any ideas?

推荐答案

您不应该需要忍受另一个搜索栏。最初的一家应该罚款。如果没有异常消息和堆栈跟踪我不知道是什么原因造成的崩溃。不过,我只是codeD的例子,可以作为你所期望的。也许通过看我的例子,你可以找出您的问题。

You shouldn't need to put up another Seekbar. The initial one should be fine. Without the exception message and stack trace I'm not sure what is causing the crash. However, I just coded an example and works as you would expect. Perhaps by looking at my example you can identify your issue.

SeekbarTest.java:

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中:

And here is the main.xml it is referencing for the layout:

<?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>

只要创建一个新的Andr​​oid应用程序,并更换产生code +布局上面的例子。它应该为你工作。

Just create a new android app and replace the generated code + layout with the example above. It should work for you.

祝你好运,
克雷格

Good luck, Craig

这篇关于更改使用不同的按钮的搜索栏的拇指位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆