第一次尝试时没有点击按钮

时间:2017-10-20 10:36:16

标签: android

我的活动中有几个按钮在第一次尝试时没有被点击,但是从第二次开始点击它们。似乎按钮会在第一次点击时获得焦点,并且从第二次开始实际按下按钮。这些按钮第一次没有被点击的原因是什么? 我注意到这种行为只能通过我活动最底层的按钮显示。

activity_main.xml中

get

review_card.xml

const jsData = '[{"x":1,"y":1},{"x":2,"y":2}]';
const imData = JSON.parse(jsData)
console.log(imData)
const Rec = Immutable.Record( imData[0])
const rec = new Rec();

console.log(rec.get('x'));

MainActivity.java

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.me.proj.view.activities.MallActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar_layout"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        app:title=" "
        android:layout_height="match_parent"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager_mall_showcase_images"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffe5e5e5"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <include layout="@layout/info_card" />
        <include layout="@layout/photo_card" />
        <include layout="@layout/address_card" />
        <include layout="@layout/opening_hours_card" />
        <include layout="@layout/review_card" /> <!-- Button in this file has the problem-->


    </LinearLayout>


</android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

2 个答案:

答案 0 :(得分:0)

我的MainActivity.class中的代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View view = findViewById(R.id.sub_layout);
        Button button = (Button) view.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Do your stuff
                Toast.makeText(MainActivity.this, "hi", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

我的activity_main.xml中的代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="eflair.myapplication.MainActivity">

    <include
        android:id="@+id/sub_layout"
        layout="@layout/sub_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>

我的sub_layout.xml中的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_name" />
</LinearLayout>

答案 1 :(得分:0)

此问题实际上是public class HomeActivity extends AppCompatActivity implements FragmentChangeListener{ ........ ....... ....... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_layout); Fragment fragment = new Appointment_Fragment(); fragment.fragmentChangeListener = this; Bundle bundle = new Bundle(); fragment.setArguments(bundle); getActivity().getSupportFragmentManager().beginTransaction() .replace(R.id.mainfrag, fragment).commit(); //Always use getChildFragmentManager() for child fragment } @Override public void onFragmentChange(String fragmentName){ if(fragmentName.equals("Appointment_Fragment")){ //do what do you want }else if(fragmentName.equals("FragmentB")){ // do what do you want }else if(fragmentName.equals("FragmentC")){ //do what do you want } } } 类中的bug一段时间了。 我解决了这个问题,我对NestedScrollView类的源代码进行了一些调整,如下所示: -

NestedScrollView

这样做之后应该可以正常工作。