使用值填充AlertDialog

时间:2015-04-24 23:38:08

标签: android android-fragments dialog

我是编程新手,并开始热衷于Android开发,这是我的第二个应用程序。

我在线查看了android教程,并试图这样做: 1.记录按钮的电位 2.单击鼠标左键打开一个对话框,提供录制选项。 3.对话框有四个按钮(开始,停止,播放,停止播放)。

工作原理: 1.单击显示一个对话框,但没有选项。 2.录制作品时,作为不同的活动播放。

我相信我错过了什么,有人可以帮忙吗?

// MainActivity.java

import android.app.AlertDialog;
import android.content.Context;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.support.v4.app.DialogFragment;
import android.view.View;


public class MainActivity extends FragmentActivity {

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void RecordClick(View v){

    RecordDialog dialog = new RecordDialog();
    dialog.show(getFragmentManager(),"Record_Dialog");
}
} 

// Dialog.java

package com.example.record;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import java.io.File;
import java.io.IOException;


public class RecordDialog extends DialogFragment{
View v;
private MediaPlayer mediaPlayer;
private MediaRecorder mediaRecorder;
private String store_file;

/*  @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

 LayoutInflater inflator = getActivity().getLayoutInflater();
    v = inflator.inflate(R.layout.customdialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(v).setPositiveButton("DONE", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
        }
    }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener(){

        @Override
       public void onClick(DialogInterface dialogInterface, int i) {

        }
    });


    return builder.create();
}
*/   
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LayoutInflater inflator = getActivity().getLayoutInflater();
    v = inflator.inflate(R.layout.customdialog, null);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    store_file= Environment.getExternalStorageDirectory()+"/audiorecorder.3gpp";
}


public void onButtonClick(View v){
    switch (v.getId()){
        case R.id.Startbtn:
            try{
                startRecording();
            }
            catch(Exception e){
                e.printStackTrace();
            }break;
        case R.id.Stopbtn:
            try{
                stopRecording();
            }
            catch(Exception e){
                e.printStackTrace();
            }break;
        case R.id.PlayBackbtn:
            try{
                startPlayBack();
            }
            catch(Exception e){
                e.printStackTrace();
            }break;
        case R.id.StopPlayingbtn:
            try{
                stopPlayBack();
            }
            catch(Exception e){
                e.printStackTrace();
            }break;
    }
}

private void startRecording() throws IOException {
    discardMediaRecorder();
    File storeFile= new File(store_file);
    if(storeFile.exists())
        storeFile.delete();

    mediaRecorder = new MediaRecorder();
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    mediaRecorder.setOutputFile(store_file);
    mediaRecorder.prepare();
    mediaRecorder.start();
}
private void discardMediaRecorder(){
    if (mediaRecorder != null)
        mediaRecorder.release();
}

private void stopRecording() {
    if (mediaRecorder != null)
        mediaRecorder.stop();
}

private void startPlayBack() throws IOException {
    discardMediaPlayer();
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setDataSource(store_file);
    mediaPlayer.prepare();
    mediaPlayer.start();
}

private void discardMediaPlayer() {
    if (mediaPlayer != null)
    {
        try{
            mediaPlayer.release();
        }catch (Exception e) {

            e.printStackTrace();
        }
    }
}

private void stopPlayBack() {
    if (mediaPlayer != null)
        mediaPlayer.stop();
}


}

// Dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical"
android:clickable="true">


<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/record"
    android:id="@+id/imageView" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start"
    android:id="@+id/Startbtn"
    android:layout_marginLeft="67dp"
    android:layout_marginStart="67dp"
    android:onClick="onButtonClick"
    android:layout_below="@+id/imageView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="70dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Stop"
    android:id="@+id/Stopbtn"
    android:layout_marginLeft="59dp"
    android:layout_marginStart="59dp"
    android:layout_alignTop="@+id/Startbtn"
    android:layout_toRightOf="@+id/Startbtn"
    android:layout_toEndOf="@+id/Startbtn"
    android:onClick="onButtonClick"
    android:clickable="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="PlayBlack"
    android:id="@+id/PlayBackbtn"
    android:onClick="onButtonClick"
    android:clickable="true"
    android:layout_centerVertical="true"
    android:layout_alignLeft="@+id/Startbtn"
    android:layout_alignStart="@+id/Startbtn" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="StopPlaying"
    android:id="@+id/StopPlayingbtn"
    android:onClick="onButtonClick"
    android:nestedScrollingEnabled="true"
    android:clickable="true"
    android:layout_centerVertical="true"
    android:layout_alignLeft="@+id/Stopbtn"
    android:layout_alignStart="@+id/Stopbtn" />
</RelativeLayout>

0 个答案:

没有答案