我基本上想要将我在XML中创建的按钮链接到相应类中的java代码。
我想要实现的是一个放在我的网页视图上方的按钮,该按钮包含文字"开始录制"单击后,音频录制功能将启动,然后文本将更改为"停止录制"。
我遇到的问题是,我的应用程序屏幕上有一个按钮,但是,它上面没有文字,当我点击按钮时我的应用程序崩溃了。
以下是该类的代码:
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);
}
});
}
这是xml的代码
<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>
答案 0 :(得分:1)
有很多方法。
例如
viewDidLoad
然后在你的班级
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RecordButton"
android:layout_above="@+id/mybrowser"
android:layout_alignParentTop="true"
android:onClick="recordClick"
android:layout_centerHorizontal="true">
如果你有多个按钮
@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);
}
});
public void recordClick(View view){
// your code to handle click
}
}
答案 1 :(得分:0)
在XML中添加OnClick方法
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RecordButton"
android:layout_above="@+id/mybrowser"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="RecordButton"
android:text="Start"
>
在Java中
Button btn = (Button) findViewById(R.id.RecordButton);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RecordButton(v);
}
});
public void RecordButton(View v) {
Button b = (Button)v;
if(b.getText().toString().equals("Start")){
onRecord(true);
b.setText("Stop");
}else{
onRecord(false);
b.setText("Start");
}
答案 2 :(得分:0)
select all_dates.value1 as `Date`, coalesce(`Outward1(B_Qty)`,`Outward2(B_Qty)`,`Outward3(B_Qty)`, '0') as `Outward`
from
(select distinct C_Date as value1 from capital where P2_Goods like '750%' and Monthname(C_Date)='April'
union all
select distinct M_Date from machine where J_Id like '%750' and Monthname(M_Date)='April'
union all
select distinct QC_Date from qc where QC_Name like '750%' and Monthname(QC_Date)='April'
) as all_dates
left join( select C_Date, sum(PR) AS `Outward1(B_Qty)`
from capital where P2_Goods like '750%' and Monthname(C_Date)='April' group by C_Date)
as f on f.C_Date = all_dates.value1
left join( select M_Date, (sum(PR)+sum(C_Skip)) AS `Outward2(B_Qty)`
from machine where J_Id like '%750' and Monthname(M_Date)='April' group by M_Date)
as h on h.M_Date = all_dates.value1
left join( select QC_Date, sum(PR) AS `Outward3(B_Qty)`
from qc where QC_Name like '750%' and Monthname(QC_Date)='April' group by QC_Date)
as k on k.QC_Date = all_dates.value1 group by all_dates.value1
实现View.OnClickLIstener
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/RecordButton"
android:layout_above="@+id/mybrowser"
android:layout_alignParentTop="true"
android:onClick="recordClick"
android:layout_centerHorizontal="true">