Android 必须点击按钮两次
在我的Android 必须点击按钮两次,android,xml,android-fragments,button,focus,Android,Xml,Android Fragments,Button,Focus,在我的main活动中我单击一个按钮打开一个片段,然后从片段我可以单击一个按钮转到第二个片段。但是当第二个片段打开时,我必须单击两次才能使按钮正常工作。我认为这与片段没有焦点有关,因为我可以单击屏幕上的任何位置,然后启用按钮,这样第二次单击将正常注册按钮单击。FAB和普通按钮都有问题 XML: 以及onClick方法: public void onClick(View v) { switch (v.getId()) { case R.id.fab:
main活动中
我单击一个按钮
打开一个片段
,然后从片段
我可以单击一个按钮
转到第二个片段
。但是当第二个片段打开时,我必须单击两次才能使按钮正常工作。我认为这与片段
没有焦点有关,因为我可以单击屏幕上的任何位置,然后启用按钮,这样第二次单击将正常注册按钮单击。FAB
和普通按钮都有问题
XML:
以及onClick方法:
public void onClick(View v) {
switch (v.getId()) {
case R.id.fab:
endFragment();
break;
case R.id.bGoBack:
endFragment();
break;
}
}
从我的第一个片段
到片段
如下两个:
getFragmentManager().beginTransaction()
.replace(R.id.container, Fragment2.newInstance())
.addToBackStack(null)
.commit();
我已经尝试将touchmode
中的focusable
设置为true,并将其设置为false。还尝试请求关注根视图
编辑
第一个片段的布局文件:
<?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
主活动布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout android:id="@+id/main_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.adrissa.image.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="snap"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@mipmap/ic_camera"
app:backgroundTint="@color/colorPrimaryDark"
app:layout_anchor="@id/container"
app:layout_anchorGravity="bottom|right|end"/>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
您是否看到onClick()
方法中的任何日志?否在我第一次单击时未调用该方法。您是否已尝试使用SupportFragmentManager
而不是FragmentManager
?我建议您发布第一个片段布局的代码和第二个片段的onCreateView方法刚刚添加了您想要的代码R.id.container
视图在哪里?
<?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>
@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;
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout android:id="@+id/main_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.adrissa.image.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="snap"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:src="@mipmap/ic_camera"
app:backgroundTint="@color/colorPrimaryDark"
app:layout_anchor="@id/container"
app:layout_anchorGravity="bottom|right|end"/>
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>