简体   繁体   English

我不明白这句话试图用JAVA告诉我什么

[英]I don't understand what this statement is trying to tell me in JAVA

Here is the description of the problem I'm trying to solve: 这是我要解决的问题的描述:

The onProgressChanged method of the ToneGenerator class gets invoked whenever the user moves either of the two sliders. 每当用户移动两个滑块中的任何一个时,就会调用ToneGenerator类的onProgressChanged方法。 Verify this by running the application connected to your laptop and use the logging window to follow the onPress messages obtained from the log. 通过运行连接到笔记本电脑的应用程序并使用日志记录窗口跟踪从日志中获取的onPress消息来验证这一点。 What is the progress value obtained when the slider is all the way to the left and all the way to the right? 当滑块一直向左移动并一直向右移动时,获得的进度值是多少?

Since the same method is invoked regardless of which slider was moved, how can we determine the one that was moved? 由于无论移动哪个滑块都调用相同的方法,我们如何确定被移动的那个? Hint: examine the parameters of the method. 提示:检查方法的参数。

One of the parameters sent to the method is seekBar, an object reference that points at the slider that was moved. 发送到该方法的参数之一是seekBar,它是一个对象引用,指向已移动的滑块。 Determine the ID of this object by using a method in its API (AndroidStudio helps with this once you type the object reference followed by a dot). 通过使用其API中的方法来确定此对象的ID(一旦键入对象引用后接点,AndroidStudio会对此提供帮助)。 Add a print statement at the top of the method to display this ID in Logcat. 在方法顶部添加打印语句,以在Logcat中显示此ID。

Save the app and re-run it on the device. 保存应用程序,然后在设备上重新运行。 Move the sliders and note the displayed ID in the log. 移动滑块并记下日志中显示的ID。

The actual ID of any control can be obtained from the layout view. 可以从布局视图中获取任何控件的实际ID。 Add a second print statement at the top of the method to display the ID's of both sliders. 在该方法的顶部添加第二个打印语句,以显示两个滑块的ID。 Recall that you must prefix the XML ID with “R.id.” when you refer to it in Java. 回想一下,当您在Java中引用XML ID时,必须在XML ID之前加上“ R.id”。

I'm having problems with the last paragraph. 我对最后一段有疑问。

Here is the code : 这是代码:

package jr.eecs1022.tonegenerator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SeekBar;

public class ToneGenerator extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener
{
    private ToneEmitter toneEmitter = null;

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
    {
        if (toneEmitter != null)
        {
            System.out.println();
            System.out.println(seekBar.getId());
            System.out.println("onPress called with " + progress);
        }
    }

    public void onStartTrackingTouch(SeekBar seekBar)
    {
    }

    public void onStopTrackingTouch(SeekBar seekBar)
    {
    }

    private void setFrequency(double frequency)
    {
        System.out.println("setFrequency called with " + frequency);
    }

    private void setAmplitude(double amplitude)
    {
        System.out.println("setAmplitude called with " + amplitude);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tone_generator);

        SeekBar seekbar = (SeekBar) findViewById(R.id.changeAmplitude);
        seekbar.setOnSeekBarChangeListener(this);
        seekbar = (SeekBar) findViewById(R.id.changeFrequency);
        seekbar.setOnSeekBarChangeListener(this);
    }

    @Override
    public void onResume()
    {
        super.onResume();
        toneEmitter = new ToneEmitter();
        this.setAmplitude(0.0);
        this.setFrequency(ToneEmitter.NOTE_C);
        SeekBar seekBar = (SeekBar) findViewById(R.id.changeAmplitude);
        seekBar.setProgress(0);
        seekBar = (SeekBar) findViewById(R.id.changeFrequency);
        seekBar.setProgress(0);
    }

    @Override
    public void onPause()
    {
        toneEmitter.shutdown();
        toneEmitter = null;
        super.onPause();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_tone_generator, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

It means that you can refer to the id of any control by using the R class. 这意味着您可以使用R类来引用任何控件的ID。

So, to try it out, it says you should add the following to the top of the onProgressChanged method: 因此,要进行尝试,它说您应该将以下内容添加到onProgressChanged方法的顶部:

System.out.println(R.id.slider1 + " " + R.id.slider2); //Use the actual id of the sliders

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM