【问题标题】:How to link a button in xml to the java actions in a class如何将xml中的按钮链接到类中的java操作
【发布时间】:2016-05-11 04:41:16
【问题描述】:

对开发非常陌生!

我基本上想将我在 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);
        }
    });

}

这是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>

【问题讨论】:

    标签: java android xml button


    【解决方案1】:

    有很多方法。

    例如

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

    【讨论】:

    • 我只有两个按钮,一个位于 webview 底部的按钮,可将我带到一个新活动 - 已编码并且工作正常(R.id.drugslaw),还有一个按钮可以处理录音功能。我相信我有正确的代码来记录(除非我没有)但是当我在我的应用程序上进入相应的屏幕时,没有出现记录功能的按钮。 @安德斯
    • 所以你说你的按钮不可见?
    • 是的,我看不到按钮
    • 我现在可以看到按钮,但是按钮上没有文字,当我点击它时,我的应用程序崩溃了
    【解决方案2】:

    在 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");
        }
    

    【讨论】:

      【解决方案3】:
      <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">
      

      实现 View.OnClickLIsener

      public class Drugs extends AppCompatActivity implements View.OnClickListener {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_drugs);
          Button btndrugs = (Button) findViewById(R.id.RecordButton);
          btndurgs.setOnClickListener(this);
            }
         public void onClick(View v) {
              switch (v.getId()){
                  case(R.id.RecordButton):
      your logic here
          break;
          }
      }}
      

      【讨论】:

      • 当我这样做时,我的 onRecord 布尔值无法解决。我正在尝试获取一个具有测试“开始录制”的按钮,当单击该按钮录制音频时,同一按钮上的文本将在单击时变为“停止录制”,它将停止录制
      • 将布尔变量设为静态并用于更改文本 your_button.setText(" ");
      猜你喜欢
      • 2019-06-23
      • 2014-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      • 2013-07-27
      • 2019-01-27
      相关资源
      最近更新 更多