我有一个类来录制音频笔记,我希望将其实现到现有活动中,但在本例中,录制和播放按钮最终会出现在屏幕顶部,并将事情搞砸。
在哪里以及如何使用以下代码指定按钮的显示属性。。。
JAVA-AudioRecordTest.JAVA
public class AudioRecordTest extends Activity
{
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName = null;
private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;
private PlayButton mPlayButton = null;
private MediaPlayer mPlayer = null;
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void onPlay(boolean start) {
if (start) {
startPlaying();
} else {
stopPlaying();
}
}
private void startPlaying() {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
private void stopPlaying() {
mPlayer.release();
mPlayer = null;
}
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);
}
}
class PlayButton extends Button {
boolean mStartPlaying = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onPlay(mStartPlaying);
if (mStartPlaying) {
setText("Stop playing");
} else {
setText("Start playing");
}
mStartPlaying = !mStartPlaying;
}
};
public PlayButton(Context ctx) {
super(ctx);
setText("Start playing");
setOnClickListener(clicker);
}
}
public AudioRecordTest() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mRecordButton = new RecordButton(this);
mPlayButton = new PlayButton(this);
setContentView(R.layout.activity_audio_record_test);
}
@Override
public void onPause() {
super.onPause();
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
}
XML-activity_audio_record_test.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ScrollView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical">
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Inspection ID" />
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/txtName"
android:inputType="number"
android:maxLength="5"
android:digits="0123456789"
android:singleLine="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text1" />
<EditText
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoText="false"
android:gravity="top|left"
android:lines="4"
android:maxLines="4"
android:minLines="4"
android:scrollbars="vertical"
android:singleLine="false"
android:width="0dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project Ref"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/txtAge"
android:inputType="number"
android:maxLength="5"
android:digits="0123456789"
android:singleLine="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Drop Down"
/>
<Spinner
android:id="@+id/spinDept"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnPhotoCamera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Take Image with Camera" />
<Button
android:id="@+id/btnPhotoGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Get Image from Gallery" />
<TextView
android:id="@+id/lblDisplayImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnCancel"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="below_this_text_image_will_be_displayed"
android:textSize="13dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/lblDisplayImage"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:gravity="bottom" >
<!--
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
-->
<ImageView
android:id="@+id/imgDisplayImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="area_where_image_is_to_be_displayed" />
<!-- </ScrollView> -->
</RelativeLayout>
<------------------- RECORD AND PLAY BUTTONS TO GO HERE ------------->
<Button
android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="btnAddEmp_Click"
android:text="Save Inspection" />
<Button
android:id="@+id/btnCancel"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_below="@+id/btnPhotoGallery"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="Reset/Clear Form Data" />
<TextView
android:id="@+id/txtEmps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number of Inspections on Device " />
</LinearLayout>
</ScrollView>
</LinearLayout>
谢谢,
henry
如果您真的需要动态创建它们,那么我们可以提供帮助。然而,看起来您每次都在onCreate()
中无条件地创建它们,所以将它们放在xml 中会容易得多
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AudioRecordTest" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<LinearLayout
android:id="@+id/btnLL"
android:layout_width="wrap_content"
android:layuout_height="wrap_content"
android:layout_alignParentBottom="true"> <!-- This property will put them at the bottom of the screen -->
<Button
android:id="@+id/recBtn"
android:layout_width="wrap_content"
android:layuout_height="wrap_content"
/>
<Button
android:id="@+id/playBtn"
android:layout_width="wrap_content"
android:layuout_height="wrap_content"
/>
</LinearLayout>
</RelativeLayout>
从onCreate()
中删除您创建的Button
和LinearLayout
并更改
setContentView(ll);
至
setContentView(R.layout.activity_audio_record_test);
您现在可以通过以下三种不同方式之一设置onClick
。一种方法是在使用buttonName.setOnClickListener( new OnclickListener(){...
初始化Button
时执行此操作。另一种方法,有时更简单、更干净的IMO,是在xml中通过将android:onClick="someFunction"
添加到Button
中来实现,然后在Java中有一个函数
public void someFunction(View v)
{
// place code to run here when that button was clicked
}
对另一个做同样的事。您也可以对它们使用相同的功能,并打开按钮id
。如果您想要