必须单击两次按钮

时间:2017-02-12 17:51:43

标签: android xml android-fragments button focus

在我的MainActivity我点击Button打开Fragment,然后从Fragment我点击Button转到第二个{ {1}}。但是当第二个Fragment打开时,我必须单击两次以使按钮工作。我认为这与Fragment没有焦点有关,因为我可以点击屏幕上的任意位置,然后启用按钮,这样第二次点击就会正常注册按钮点击。 Fragment和普通按钮都有问题。

XML:

FAB

在我的<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="match_parent" android:contentDescription="@string/capture_image"/> <Button android:id="@+id/bGoBack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="16dp" android:contentDescription="@string/camera_error" android:drawableStart="@mipmap/ic_back_arrow" android:background="@android:color/transparent"/> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="16dp" android:src="@mipmap/ic_action_send" app:backgroundTint="@color/colorPrimaryDark" app:layout_anchor="@id/iv" app:layout_anchorGravity="bottom|right|end"/> </android.support.design.widget.CoordinatorLayout> 中,我初始化了FragmentFAB,并指定了点击监听器:

Button

onClick方法:

mFab.setOnClickListener(this);
mExitButton.setOnClickListener(this);

从我的第一个public void onClick(View v) { switch (v.getId()) { case R.id.fab: endFragment(); break; case R.id.bGoBack: endFragment(); break; } } 我转到Fragment两个像这样:

Fragment

我已尝试将getFragmentManager().beginTransaction() .replace(R.id.container, Fragment2.newInstance()) .addToBackStack(null) .commit(); 中的focusablefocusable设置为true并将其设置为false。还试图请求关注根视图。

修改

第一个片段的布局文件:

touchmode

第二个片段的OnCreate方法:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <com.adrissa.localsnap.AutoFitTextureView
        android:id="@+id/texture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"/>

    <ImageButton
        android:id="@+id/picture"
        style="@android:style/Widget.Material.Light.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="@dimen/image_capture_top_bottom_margin"
        android:contentDescription="@string/capture_image"
        android:src="@drawable/button_capture_image"
        android:background="@android:color/transparent"/>

</RelativeLayout>

编辑2

MainActivity布局文件

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // inflate layout resource
        View view = inflater.inflate(R.layout.fragment2,
                container, false);

        // set status bar color to invisible to take and show image in full screen.
        getActivity().getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

        // initialize views
        mImageView = (ImageView) view.findViewById(R.id.iv);
        mFab = (FloatingActionButton) view.findViewById(R.id.fab);
        mExitButton = (Button) view.findViewById(R.id.bGoBack);

        //set onClickListeners
        mFab.setOnClickListener(this);
        mExitButton.setOnClickListener(this);

        // Get screen size as imageView will fill entire screen.
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int targetW = size.x;
        int targetH = size.y;

        // Get the path of the captured image
        String mCurrentPhotoPath = ((MainActivity) getActivity()).getImagePath();

        // Get the dimensions of the bitmap
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
        int photoW = bmOptions.outWidth;
        int photoH = bmOptions.outHeight;

        Log.d("bitmap", "target w" + targetW + " target h" + targetH + " photo w" + photoW + " photo h" + photoH);

        // Determine how much to scale down the image
        int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

        // Decode the image file into a Bitmap sized to fill the View
        bmOptions.inJustDecodeBounds = false;
        bmOptions.inSampleSize = scaleFactor;
        bmOptions.inPurgeable = true;

        Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

        mImageView.setImageBitmap(bitmap);

        return view;
    }

0 个答案:

没有答案