2016-05-11 26 views
1

非常新的開發!如何將xml中的按鈕鏈接到類中的java動作

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

} 

這是

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

回答

1

有很多種方法對XML的代碼。

例如

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

我只有兩個按鈕,那個帶我到一個新的活動的WebView的底部有一個按鈕 - 這是編碼和工作正常(R.id.drugslaw),並有一個按鈕,處理記錄功能。 我相信我有正確的代碼來記錄(除非我沒有),但是當我在我的應用程序中輸入相應的屏幕時,沒有出現記錄功能的按鈕。 @Anders – Viverson

+0

所以你說你按鈕不可見? – Max

+0

是的,我不能看到按鈕 – Viverson

0

添加OnClick方法在XML

<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"); 
    } 
0
<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.OnClickLIstener

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; 
    } 
}} 
+0

當我這樣做時,我的onRecord布爾值無法解決。我試圖得到一個測試「開始錄製」的按鈕,當點擊錄製音頻時,同一個按鈕上的文本在點擊時會變成「停止錄製」,它將停止錄製 – Viverson

+0

使您的布爾變量變爲靜態並且用於更改text your_button.setText(「」); –

相關問題