gpt4 book ai didi

Android - 如何通过调用 seekbar 方法来定义值?

转载 作者:行者123 更新时间:2023-11-30 03:21:32 26 4
gpt4 key购买 nike

我正在尝试通过调用搜索栏中的方法来设置一个值。

我调用方法 getProgress() 并使应用程序崩溃。

我在扩展 View 的 Activity 中这样做。

搜索栏值在 MainActivity 上设置。

public class Draw extends View 
{
public Draw(Context context)
{
super(context);
}

Paint prop = new Paint();
Random color = new Random();

SeekBar bar = (SeekBar) findViewById(R.id.skbContrast);
int value = bar.getProgress();// it crashes here

@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);

int width = getWidth();
int height = getHeight();

int oriwidth = 0;
int oriheight = 0;

for (int x = 0; x < 20; x++)
{
int red = color.nextInt(value);
int green = color.nextInt(value);
int blue = color.nextInt(value);

prop.setARGB(255, red, green, blue);
canvas.drawRect(oriwidth += 10, oriheight += 10, width -= 10, height -= 10, prop);
}
}
}

有什么帮助吗?谢谢,对不起英语。

最佳答案

SeekBar bar = (SeekBar) findViewById(R.id.skbContrast);

This will return null when you try to reference it into View.

相反,您应该采取以下方法。

将以下语句移至您的 Activity 。

SeekBar bar = (SeekBar) findViewById(R.id.skbContrast);
int value = bar.getProgress();

在您的 View 类中创建一个方法。

public void setSeekBarValue(long seekBarValue)
{
this.seekBarValue=seekBarValue;
}

每当您想传递值以查看时,从 MainActivity 编写以下代码段。

SeekBar bar = (SeekBar) findViewById(R.id.skbContrast);
int value = bar.getProgress();
viewObject.setSeekBarValue(value);

这不是完整的解决方案,只是一个帮助您入门的想法。

关于Android - 如何通过调用 seekbar 方法来定义值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19036389/

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