我正在尝试在onClickListener中使用for循环来获取按钮。但是如果我使用cursorCount
变量作为条件,它就不会执行onClick()
方法中的任何代码。如果我将cursorCount
与5
这样的固定值进行交换,则效果非常好。我也可以在for循环之前的任何给定点访问cursorCount
的值。每次特定事件发生时cursorCount
都会增加,这样可以正常工作。按钮定义:
<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()");
}
}
});
}
cursorCount
此处更改了值:
public void checkValue(){
if(osmdMidiValue == playedMidiValue){
webView.loadUrl("javascript:nextValue()");
cursorCount++;
getMidiValue();
}
else if(osmdMidiValue == 0){
webView.loadUrl("javascript:nextValue()");
cursorCount++;
getMidiValue();
}
}
答案 0 :(得分:0)
使用局部变量而不是字段,以便每个块都以所需的计数开始。