在onClickListener中使用for循环

问题描述 投票:0回答:1

我试图在onClickListener中使用for循环来获取一个按钮。但是如果我使用cursorCountvariable作为条件,它不会执行onClick()方法中的任何代码。如果我交换cursorCount与固定值像5it工作完全正常。我也可以在for循环之前访问任何给定点的cursorCountat的值。 cursorCountis每次特定事件发生时都会增加,这很好。按钮定义:

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Previous"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/button"
    app:layout_constraintHorizontal_bias="0.181"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/activity_main_webview"
    app:layout_constraintVertical_bias="0.556" />

活动/的onCreate:

public class MainActivity extends AppCompatActivity {

private WebView webView;
private static final int MY_PERMISSIONS_RECORD_AUDIO = 693;
TextView noteText;
TextView pitchText;
String midiString;
int osmdMidiValue = 1;
int playedMidiValue = 1;
public int cursorCount;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    noteText = findViewById(R.id.noteText);
    pitchText = findViewById(R.id.textView);

    // Check if permission is not granted
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.RECORD_AUDIO)
            != PackageManager.PERMISSION_GRANTED) {

        // Shows Pop-up
        ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.RECORD_AUDIO},
            MY_PERMISSIONS_RECORD_AUDIO);

    }
    else{
        AudioDispatcher dispatcher =
                AudioDispatcherFactory.fromDefaultMicrophone(22050, 1024, 0);
        PitchDetectionHandler pdh = new PitchDetectionHandler() {
                @Override
                public void handlePitch(PitchDetectionResult res, AudioEvent e) {
                    final float pitchInHz = res.getPitch();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            processPitch(pitchInHz);
                            if(pitchInHz!=-1) {
                                pitchText.setText(String.valueOf(pitchInHz));
                            }
                                    }
                        });
                 }
        };
        AudioProcessor pitchProcessor =
                new PitchProcessor(PitchProcessor.PitchEstimationAlgorithm.FFT_YIN, 22050, 1024, pdh);
        dispatcher.addAudioProcessor(pitchProcessor);

        Thread audioThread = new Thread(dispatcher, "Audio Thread");
        audioThread.start();
    }


    startDisplay();

    final Button startbtn = findViewById(R.id.button);
    startbtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            getMidiValue();
        }
    });

    final Button resetbtn = findViewById(R.id.button2);
    resetbtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            webView = findViewById(R.id.activity_main_webview);
            webView.loadUrl("javascript:resetCursor()");
            cursorCount=0;
        }
    });

    final Button prevbtn = findViewById(R.id.button3);
    prevbtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int test = cursorCount;
            webView = findViewById(R.id.activity_main_webview);
            webView.loadUrl("javascript:resetCursor()");
            for(int prevCount=0; prevCount<test; prevCount++){
                webView.loadUrl("javascript:nextValue()");
            }
        }
    });

}

cursorCountValue在这里更改:

public void checkValue(){

    if(osmdMidiValue == playedMidiValue){
        webView.loadUrl("javascript:nextValue()");
        cursorCount++;
        getMidiValue();

    }
    else if(osmdMidiValue == 0){
        webView.loadUrl("javascript:nextValue()");
        cursorCount++;
        getMidiValue();
    }

}
java android
1个回答
0
投票

使用局部变量而不是字段,以便每个块以所需的计数开始。

© www.soinside.com 2019 - 2024. All rights reserved.