这可能是一个非常愚蠢的问题(它有99%的可能性),但我无法想象我的生活。我在一个活动中有一个搜索栏,我们称之为活动1 ,我需要在另一个活动中使用它不断变化的值(进度),我们称之为活动2 ,用于计算(或者我可以在第一个活动中进行计算,然后将值发送到第二个,IDK会更好)。
所以这是我的活动1 代码:
public class Painting extends Activity
{
SeekBar curveBar;
SampleView sampleView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_painting);
curveBar = (SeekBar)findViewById(R.id.curveBar);
sampleView = new SampleView(this);
curveBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int progressChangedValue = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (progress >= 50) //prints out 50, 60, 70, 80, 90, 100
{
progressChangedValue = progress;
}
else if (progress < 50) //prints out -40, -30, -20, -10, 0
{
progressChangedValue = (-1) * (progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(Painting.this, "Value: " + progressChangedValue, Toast.LENGTH_SHORT).show();
}
});
}
}
这是我的RELEVANT 活动2 代码:
public class SampleView extends View
{
private Paint mPaint;
private float mX;
private float[] mPos;
private Path mPath;
private Paint mPathPaint;
private static final int DY = 30;
private static final String TEXTONPATH = "Along a path";
public SampleView(Context context, AttributeSet attrs)
{
super(context, attrs);
setFocusable(true);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setTextSize(90);
mPaint.setTypeface(Typeface.SERIF);
mPath = new Path();
makePath(mPath);
mPathPaint = new Paint();
mPathPaint.setAntiAlias(true);
mPathPaint.setColor(Color.rgb(255,0,0));
mPathPaint.setStyle(Paint.Style.STROKE);
}
public SampleView(Context context)
{
super(context);
setFocusable(true);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setTextSize(90);
mPaint.setTypeface(Typeface.SERIF);
mPath = new Path();
makePath(mPath);
mPathPaint = new Paint();
mPathPaint.setAntiAlias(true);
mPathPaint.setColor(Color.rgb(255,0,0));
mPathPaint.setStyle(Paint.Style.STROKE);
}
public void makePath(Path p) //<----- this is where i need the seekbar's value
//so that i can make this take more variables so that the seekbar
//adjusts the values in the p.cubicTo's below
{
// p.moveTo(250, -300);
p.moveTo(0,0);
// p.cubicTo(-250, -550, 750, -550, 250, -300);
p.cubicTo(0,-400,600,-400,600,0); //semi-circle?
// p.cubicTo(-600, -400, 600, -400, 0, 0); //as far as the curve probably should allow
// p.cubicTo(0, 0, 0, 0, 620,0); //flat line
}
我试图在第一个活动和第二个活动上使用curvebar.getProgress()
,但除非我在OnSekbarChangeListener
内,否则我无法得到它。我已经尝试将公共变量设置为值,但只有在我在侦听器中设置值时它才会改变,否则它不会(这非常有意义)。我已经尝试将搜索栏设置为静态(尽管我可能没有做到这一点)并且这也不起作用。我只是无法弄清楚如何获得该值并在第二个文件中使用它。
我真的很感激一些帮助
谢谢
答案 0 :(得分:1)
只需在SampleView类中创建一个公共方法,该方法可以将搜索栏的进度作为参数。
public class SampleView extends View {
...
public void updatePath(int seekBarProgress) {
// Do whatever you need to with the passed in value here.
}
...
}
然后在您的Activity中,您可以调用此方法以使用最新的进度值更新SampleView。
public class Painting extends Activity {
...
curveBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
...
sampleView.updatePath(progress);
...
}
...
}
}