如何在Android Studio项目中实现以下OnClick代码?

时间:2016-04-02 15:45:37

标签: android android-studio

以下代码来自Android开发者'在以下URL处构建摄像机的指南。请花点时间帮我解决这个问题。最好的祝愿! The aforementioned site

错误似乎是:

  1. 我在id ...
  2. 之前手动添加R.
  3. setOnclickListener,setCaptureButtonText,View v,@ Override和mMediaRecorder显示为错误。
  4. The)最后也被突出显示为错误。
  5. 这是来自activity.xml文件的代码,显示了被引用的按钮:

    <Button
        android:id="@+id/button_capture"
        android:text="Capture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />
    

    以下代码本身:

        private boolean isRecording = false;
    
        // Add a listener to the Capture button
        Button captureButton = (Button) findViewById(id.button_capture);
        captureButton.setOnClickListener(
        new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isRecording) {
                // stop recording and release camera
                mMediaRecorder.stop();  // stop the recording
                releaseMediaRecorder(); // release the MediaRecorder object
                mCamera.lock();         // take camera access back from MediaRecorder
    
                // inform the user that recording has stopped
                setCaptureButtonText("Capture");
                isRecording = false;
            } else {
                // initialize video camera
                if (prepareVideoRecorder()) {
                    // Camera is available and unlocked, MediaRecorder is prepared,
                    // now you can start recording
                    mMediaRecorder.start();
    
                    // inform the user that recording has started
                    setCaptureButtonText("Stop");
                    isRecording = true;
                } else {
                    // prepare didn't work, release the camera
                    releaseMediaRecorder();
                    // inform user
                }
            }
        }
        }
        );
    

1 个答案:

答案 0 :(得分:0)

由于您使用的是XML,因此您应该直接将属性添加到对象中。无论你告诉它运行什么功能都会运行。无需添加任何其他侦听器。

<Button
    android:id="@+id/button_capture"
    android:text="Capture"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:onClick="yourFunction"
    />