2016-02-09 144 views
0

我遇到了無法點擊的按鈕問題,我不知道爲什麼。請有人幫忙/告訴我嗎?按鈕無法點擊

下面是我使用的代碼:我不知道爲什麼我的按鈕是無法點擊

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

,有人請借給我伸出援助之手。

+1

您的邏輯不正確,至少首先啓用一個按鈕。 – droidev

回答

0

你自己做到了。

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

要禁用它,並使它在ButtononClick,如何如果Button是禁用onClick會叫什麼名字?

ATLEAST enable一個按鈕,這樣其他的代碼將得到執行

+1

這有幫助。我忘了啓用SpeakBtn,並感謝提醒我! –

0

設置你的按鈕中的一個真正的,因爲你是禁用按鈕。

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

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

所以你的代碼將得到執行。

0

onClick of按鈕只有在啓用按鈕時纔有效。首先在您要使用onClick按鈕的位置啓用按鈕。 從下面刪除最後兩行

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

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