我试图在Google Camera2Basic示例代码中了解camera2 api的工作方式。具体来说,“图片”按钮如何注册镜头?

在onCreateViewCreated中:

@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
    view.findViewById(R.id.picture).setOnClickListener(this);
    view.findViewById(R.id.info).setOnClickListener(this);
    mTextureView = (AutoFitTextureView) view.findViewById(R.id.texture);
}

那么setOnClickListener()注册点击?但是它会去什么呢?我看到传递了这个的,但我不知道发生了什么。

我通常要做的事情是在onCreateView()中设置一个按钮,并将其setOnClickListener()连接到某个动作,例如:
photoButton = (Button)v.findViewById(R.id.picture);
photoButton.setOnClickListener(new View.onSetClickListener() {
    @Override
    public void onClick(View v) {
        //some action
    }
});

最佳答案

示例代码中也发生了同样的事情。但是,它看起来有点不同,因为Camera2BasicFragment Activity 正在实现OnClickListener。因此,在设置onClickListener时,给出this表示此 Activity 将覆盖onClick方法。因此,当单击按钮时,将自动调用该类中的onClick方法。

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.picture: {
            takePicture();
            break;
        }
        case R.id.info: {
            Activity activity = getActivity();
            if (null != activity) {
                new AlertDialog.Builder(activity)
                        .setMessage(R.string.intro_message)
                        .setPositiveButton(android.R.string.ok, null)
                        .show();
            }
            break;
        }
    }
}

07-24 09:47
查看更多