如何以编程方式插入两个按钮?

时间:2011-08-05 09:37:24

标签: android button android-layout

我目前正在处理其他人编写的代码。我可以看到他实现了下面的记录器布局

recorder = new MediaRecorder();
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

mPreview = new Preview(VideoRecorder.this,recorder);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(mPreview);

我的问题是我想以编程方式将两个Button插入此mPreview进行记录并停止。

我该怎么做?

全班如下

public class VideoRecorder extends Activity {

    private SQLiteDatabase db;
    public ArrayList<String> videocount = new ArrayList<String>();

    public static int x = 1;

    public static boolean recod = false;
    public static boolean video = true;

    public boolean onCreateOptionsMenu(Menu menu) {

        // TODO Auto-generated method stub
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(a.b.R.menu.menus, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case id.record:

            if (video) {

                recorder.start();
                recod = true;
                video = false;
            } else {
                Toast.makeText(getApplicationContext(),
                        "Stop Recording First..", 2000).show();
            }

            break;
        case id.stop:
            if (recod) {

                String outputfilepath = "/sdcard/RdmsVideo" + x + ".3gpp";
                recorder.stop();
                recorder.release();
                recorder = null;
                video = true;
                this.db = this.openOrCreateDatabase("filestore", MODE_PRIVATE,
                        null);
                this.db.execSQL("CREATE TABLE IF NOT EXISTS videos(urls text)");
                db.execSQL("insert into videos values ('" + outputfilepath
                        + "')");
                db.close();
                x++;

                // Toast.makeText(getApplicationContext(), outputfilepath,
                // 2000).show();
                Intent i = new Intent(VideoRecorder.this, sendingpage.class);
                startActivity(i);
                this.finish();

            } else {
                Toast.makeText(getApplicationContext(), "Record First", 2000)
                        .show();
            }

            // Intent intent = getIntent();
            // finish();
            // startActivity(intent);
            break;

        }
        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:

            homepage ff = new homepage();
            if (ff.vid == 1) {
                Intent i = new Intent(VideoRecorder.this, homepage.class);
                startActivity(i);
                this.finish();
            } else {
                Intent i = new Intent(VideoRecorder.this, tabpage.class);
                startActivity(i);
                this.finish();
            }

            break;

        default:
            break;
        }
        return super.onKeyDown(keyCode, event);
    }

    // Create objects of MediaRecorder and Preview class
    private MediaRecorder recorder;
    private Preview mPreview;

    boolean flag = false;
    boolean startedRecording = false;
    boolean stoppedRecording = false;

    // In this method, create an object of MediaRecorder class. Create an object
    // of
    // RecorderPreview class(Customized View). Add RecorderPreview class object
    // as content of UI.
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        recorder = new MediaRecorder();
        recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

        mPreview = new Preview(VideoRecorder.this, recorder);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(mPreview);
        final LayoutParams lparams = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        final Button record = new Button(VideoRecorder.this); // Activity goes
                                                                // here
        record.setText("Record");
        record.setLayoutParams(lparams);
        // record.setOnClickListener(...);
        //mPreview.add

    }

    /*
     * ! <p> Initialize the contents of the Activity's standard options menu.
     * Menu items are to be placed in to menu. This is called on each press of
     * menu button. In this options to start and stop recording are provided.
     * Option for start recording has group id 0 and option to stop recording is
     * 1. (first parameter of menu.add method). Start and stop have different
     * group id, if recording is already started then it shows stop option else
     * it shows start option. </p>
     */

    /*
     * ! <p> This method receives control when Item in menu option is selected.
     * It contains implementations to be performed on selection of menu item.
     * </p>
     */

    class Preview extends SurfaceView implements SurfaceHolder.Callback {
        // Create objects for MediaRecorder and SurfaceHolder.
        SurfaceHolder mHolder;
        MediaRecorder tempRecorder;

        // Create constructor of Preview Class. In this, get an object of
        // surfaceHolder class by calling getHolder() method. After that add
        // callback to the surfaceHolder. The callback will inform when surface
        // is
        // created/changed/destroyed. Also set surface not to have its own
        // buffers.
        public Preview(Context context, MediaRecorder recorder) {
            super(context);
            tempRecorder = recorder;
            mHolder = getHolder();
            mHolder.addCallback(this);
            mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

            // TODO Auto-generated constructor stub
        }

        public Surface getSurface() {
            return mHolder.getSurface();
        }

        // Implement the methods of SurfaceHolder.Callback interface

        // SurfaceCreated : This method gets called when surface is created.
        // In this, initialize all parameters of MediaRecorder object.
        // The output file will be stored in SD Card.

        public void surfaceCreated(SurfaceHolder holder) {

            tempRecorder.setOutputFile("/sdcard/RdmsVideo" + x + ".3gpp");
            tempRecorder.setPreviewDisplay(mHolder.getSurface());
            try {
                tempRecorder.prepare();
            } catch (Exception e) {
                String message = e.getMessage();
                tempRecorder.release();
                tempRecorder = null;
            }
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            if (tempRecorder != null) {
                tempRecorder.release();
                tempRecorder = null;
            }
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int w,
                int h) {

        }
    }
}

2 个答案:

答案 0 :(得分:2)

public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {        
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    final Button record = new Button(this); //Activity goes here
    record.setText("Record");
    record.setLayoutParams(lparams);
    record.setOnClickListener(...);
    parent.addView(record);
}

答案 1 :(得分:0)

我在这个

中选择了另一个例子

案例R.id.btnRecord:

{
AudioLog.logString(“开始录制”);

((按钮)findViewById(R.id.btnStart))setVisibility(View.GONE);

((按钮)findViewById(R.id.btnStop))setVisibility(View.VISIBLE);

enableButtons(真);

的startRecording();

中断;

}

案例R.id.btnStop:

{

AudioLog.logString(“停止录制”);

((按钮)findViewById(R.id.btnStop))setVisibility(View.GONE);

                ((Button)findViewById(R.id.btnStart)).setVisibility(View.VISIBLE);

                enableButtons(false);

                stopRecording();

                break;

}

,xml部分是

    <Button

        android:id="@+id/btnStop" android:textStyle="bold" android:textColor="#ffffff" android:textSize="17sp" 
        android:text="Stop" android:background="@drawable/allotherbuttonstop"
        android:layout_weight="1.0"/>