非常新的开发!如何将xml中的按钮链接到类中的java动作
我基本上想要将我在XML中创建的按钮链接到相应类中的java代码。
我试图实现的是一个按钮,放在我的web视图上有文本“开始录制”一旦点击音频记录功能将开始,文本将更改为“停止录制”。
我遇到的问题是我的应用程序屏幕上有一个按钮,但是没有文本,当我点击按钮时,我的应用程序崩溃了。
下面是类代码:
public class Drugs extends AppCompatActivity {
WebView myBrowser;
private static final String LOG_TAG = "Recording";
private static String mFileName = null;
private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
class RecordButton extends Button {
boolean mStartRecording = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onRecord(mStartRecording);
if (mStartRecording) {
setText("Stop recording");
} else {
setText("Start recording");
}
mStartRecording = !mStartRecording;
}
};
public RecordButton(Context ctx) {
super(ctx);
setText("Start recording");
setOnClickListener(clicker);
}
}
public Drugs() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drugs);
myBrowser = (WebView) findViewById(R.id.mybrowser);
myBrowser.loadUrl("file:///android_asset/drugs.html");
myBrowser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
Button btndrugslaw = (Button) findViewById(R.id.drugslaw);
btndrugslaw.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intentdruglaw = new Intent(Drugs.this, DrugLaw.class);
startActivity(intentdruglaw);
}
});
}
这是
<ScrollView
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="kyr.com.knowyourrights.Drugs"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RecordButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="onClick"
>
</Button>
<WebView
android:id="@+id/mybrowser"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</WebView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/drugslaw"
android:text="Take me to the law"
android:layout_below="@id/mybrowser"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</ScrollView>
我只有两个按钮,那个带我到一个新的活动的WebView的底部有一个按钮 - 这是编码和工作正常(R.id.drugslaw),并有一个按钮,处理记录功能。 我相信我有正确的代码来记录(除非我没有),但是当我在我的应用程序中输入相应的屏幕时,没有出现记录功能的按钮。 @Anders – Viverson
所以你说你按钮不可见? – Max
是的,我不能看到按钮 – Viverson