java - 如何将 xml 中的按钮链接到类中的 java 操作

标签 java android xml button

非常新的发展!

我基本上想将我在 XML 中创建的按钮链接到相应类中的 java 代码。

我想要实现的是放置在我的 webview 上方的按钮,该按钮具有文本“开始录制”,一旦单击音频录制功能将启动,然后文本将更改为“停止录制”。

我遇到的问题是,我的应用程序屏幕上有一个按钮,但是上面没有文本,当我单击该按钮时,我的应用程序崩溃了。

类的代码如下:

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>

最佳答案

方法有很多。

例如

<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
}

}

如果你有多个按钮

    public void recordClick(View view){

    switch(view.getId())

    //HERE YOU WILL PASS THE ID OF YOUR BUTTONS
    case R.id.firstButton:
        //Code to handle click
    case R.id.secondButton:
        //Code to handle second button click

    // AND SO ON
}

关于java - 如何将 xml 中的按钮链接到类中的 java 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37153256/

相关文章:

java - 是否有一种可移植的方式来获得 "SELECT FIRST 10 * FROM T"语义?

java - Jackson将java组合序列化为json

java - 将列表值流式传输到 Map.Entry.values

Java dom document.getElementsByTagName ("") 返回 null?

java - 使用 “image:image” 创建 XML 元素

java - Apache Jackrabbit - 重复节点?

android - 什么是应与3.6.1 Android Studio一起使用的Gradle版本

android - RecyclerView 删除项目不调用 OnScrollListener

java - 如何在Android中上传音频、文本或图像数据?

java - 解析 XML 时将子节点与控制值进行比较