Android 按钮无法单击
我的按钮有问题,无法点击,我不知道为什么。有人能帮忙/告诉我吗 以下是我使用的代码:Android 按钮无法单击,android,button,Android,Button,我的按钮有问题,无法点击,我不知道为什么。有人能帮忙/告诉我吗 以下是我使用的代码: package com.example.randallinho.saling_wika; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; impo
package com.example.randallinho.saling_wika;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
public class RecordModule extends Activity {
Button SpeakBtn, StopBtn;
private MediaRecorder myAudioRecorder;
private String outputFile = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recordmodule);
SpeakBtn = (Button) findViewById(R.id.SpeakBtn);
StopBtn = (Button) findViewById(R.id.StopBtn);
StopBtn.setEnabled(false);
SpeakBtn.setEnabled(false);
outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";
myAudioRecorder= new MediaRecorder();
myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);
SpeakBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
myAudioRecorder.prepare();
myAudioRecorder.start();
}
catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
SpeakBtn.setEnabled(false);
StopBtn.setEnabled(true);
Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
}
});
StopBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAudioRecorder.stop();
myAudioRecorder.release();
myAudioRecorder = null;
StopBtn.setEnabled(false);
SpeakBtn.setEnabled(true);
Toast.makeText(getApplicationContext(), "Audio recorded successfully", Toast.LENGTH_LONG).show();
}
});
SpeakBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) throws IllegalArgumentException,SecurityException,IllegalStateException {
MediaPlayer m = new MediaPlayer();
try {
m.setDataSource(outputFile);
}
catch (IOException e) {
e.printStackTrace();
}
try {
m.prepare();
}
catch (IOException e) {
e.printStackTrace();
}
m.start();
Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show();
}
});
}
@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_record_module, 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);
}
我不知道为什么我的按钮无法点击,请有人帮我一下。是你自己做的
StopBtn.setEnabled(false);
SpeakBtn.setEnabled(false);
您正在禁用它并在该按钮的onClick
中启用它,如果按钮被禁用,将如何调用onClick
至少启用一个按钮,这样其他代码将得到执行,因为您正在禁用按钮,所以请将其中一个按钮设置为true
SpeakBtn = (Button) findViewById(R.id.SpeakBtn);
StopBtn = (Button) findViewById(R.id.StopBtn);
StopBtn.setEnabled(true);
SpeakBtn.setEnabled(false);
因此,您的代码将得到执行。只有在按钮启用时,单击按钮才能工作。首先在要单击按钮的位置启用按钮。
从下面拆下最后两行
SpeakBtn = (Button) findViewById(R.id.SpeakBtn);
StopBtn = (Button) findViewById(R.id.StopBtn);
StopBtn.setEnabled(false);
SpeakBtn.setEnabled(false);
您的逻辑不正确,最初至少启用一个按钮。这有帮助。我忘了启用那个SpeakBtn,谢谢你提醒我!