按钮无法点击

时间:2016-02-09 12:34:55

标签: android button

我的按钮出现问题,无法点击,我不知道为什么会这样。有人可以帮忙/告诉我吗?

以下是我使用的代码:

package com.example.randallinho.saling_wika;


import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.IOException;

public class RecordModule extends Activity {

Button SpeakBtn, StopBtn;
private MediaRecorder myAudioRecorder;
private String outputFile = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recordmodule);

    SpeakBtn = (Button) findViewById(R.id.SpeakBtn);
    StopBtn = (Button) findViewById(R.id.StopBtn);

    StopBtn.setEnabled(false);
    SpeakBtn.setEnabled(false);
    outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";

    myAudioRecorder= new MediaRecorder();
    myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
    myAudioRecorder.setOutputFile(outputFile);

    SpeakBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                myAudioRecorder.prepare();
                myAudioRecorder.start();
            }

            catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            SpeakBtn.setEnabled(false);
            StopBtn.setEnabled(true);

            Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
        }
    });

    StopBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            myAudioRecorder.stop();
            myAudioRecorder.release();
            myAudioRecorder = null;

            StopBtn.setEnabled(false);
            SpeakBtn.setEnabled(true);

            Toast.makeText(getApplicationContext(), "Audio recorded successfully", Toast.LENGTH_LONG).show();
        }
    });

    SpeakBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) throws IllegalArgumentException,SecurityException,IllegalStateException {
            MediaPlayer m = new MediaPlayer();

            try {
                m.setDataSource(outputFile);
            }

            catch (IOException e) {
                e.printStackTrace();
            }

            try {
                m.prepare();
            }

            catch (IOException e) {
                e.printStackTrace();
            }

            m.start();
            Toast.makeText(getApplicationContext(), "Playing audio", Toast.LENGTH_LONG).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_record_module, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

我不知道为什么我的按钮无法点击,有人请帮助我。

3 个答案:

答案 0 :(得分:0)

你自己做到了。

StopBtn.setEnabled(false);
SpeakBtn.setEnabled(false);

您正在禁用它并在onClick的{​​{1}}中启用它,如果Button被禁用,将如何调用onClick

Atleast Button一个按钮,以便其他代码可以执行

答案 1 :(得分:0)

将您的某个按钮设置为true,因为您正在禁用按钮。

SpeakBtn = (Button) findViewById(R.id.SpeakBtn);
StopBtn = (Button) findViewById(R.id.StopBtn);

StopBtn.setEnabled(true);
SpeakBtn.setEnabled(false);

所以你的代码会被执行。

答案 2 :(得分:0)

onClick of按钮仅在启用按钮时有效。首先启用按钮,然后单击按钮。 从下面删除最后两行

SpeakBtn = (Button) findViewById(R.id.SpeakBtn);
StopBtn = (Button) findViewById(R.id.StopBtn);

StopBtn.setEnabled(false);
SpeakBtn.setEnabled(false);