按钮未被点击

时间:2014-10-08 05:43:05

标签: android button

在我的应用程序中,我正在同一个活动中执行动画,RelativeLayout关闭,另一个RelativeLayout出现。出现的布局包含一个按钮,当我尝试单击该按钮以移动到下一页时,按钮未被单击。因此应用程序挂起。

MainActivity.java

public class P3 extends Activity {
    RelativeLayout rel, rel1, rel2, rel4;
    Animation slide_down, slide_center, slide_up, slide_top;
    AnimationDrawable frameAnimation;
    ViewFlipper flippy;
    int i;
    Button b;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.p3);
        int currentOrientation = getResources().getConfiguration().orientation;
        rel1 = (RelativeLayout) findViewById(R.id.Rel_lay_1_p3);
        if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
            context = this;
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.lay_inflate_land, null);
            rel1.addView(view);
        } else {
            context = this;
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.lay_inflate, null);
            rel1.addView(view);
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                context = getApplicationContext();
                int currentOrientation = getResources().getConfiguration().orientation;
                if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
                    LayoutInflater inflater = LayoutInflater.from(context);
                    View view = inflater.inflate(
                            R.layout.green_lay_inflate_land, null);
                    rel1.addView(view);

                } else {
                    context = getApplicationContext();
                    LayoutInflater inflater = LayoutInflater.from(context);
                    View view = inflater.inflate(R.layout.green_lay_inflate,
                            null);
                    rel1.addView(view);
                }
            }
        }, 500);

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                int currentOrientation = getResources().getConfiguration().orientation;
                if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                    i = 1;
                } else {
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                    i = 2;
                }
                rel = (RelativeLayout) findViewById(R.id.Rel_lay_1_p3_n);
                slide_down = AnimationUtils.loadAnimation(
                        getApplicationContext(), R.anim.go_down);
                rel.startAnimation(slide_down);

                slide_center = AnimationUtils.loadAnimation(
                        getApplicationContext(), R.anim.go_down);
                slide_center.setAnimationListener(animationListener);
                rel1.startAnimation(slide_center);
            }
        }, 1000);

    }

    private AnimationListener animationListener = new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

            rel2 = (RelativeLayout) findViewById(R.id.Rel_lay_2_p3);

            rel4 = (RelativeLayout) findViewById(R.id.Rel_lay_2_1_p3);
            rel4.setVisibility(View.INVISIBLE);
            if (i == 1) {
                LayoutInflater inflater = LayoutInflater.from(context);
                View view = inflater.inflate(R.layout.green_lay_inflate_land,
                        null);

                rel2.addView(view);
                rel.setVisibility(View.INVISIBLE);
                rel2.setVisibility(View.VISIBLE);

                inflater = LayoutInflater.from(context);
                view = inflater.inflate(R.layout.lay_inflate_land, null);

                rel4.addView(view);
            } else {
                LayoutInflater inflater = LayoutInflater.from(context);
                View view = inflater.inflate(R.layout.green_lay_inflate, null);

                rel2.addView(view);
                rel.setVisibility(View.INVISIBLE);
                rel2.setVisibility(View.VISIBLE);

                inflater = LayoutInflater.from(context);
                view = inflater.inflate(R.layout.lay_inflate, null);

                rel4.addView(view);
            }
            flippy = (ViewFlipper) findViewById(R.id.viewFlipper1);
            flippy.setFlipInterval(500);
            flippy.startFlipping();
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    flippy.stopFlipping();

                }
            }, 5000);

            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {

                    rel2.setVisibility(View.VISIBLE);
                    rel4.setVisibility(View.INVISIBLE);

                    if (i == 1) {
                        LayoutInflater inflater = LayoutInflater.from(context);
                        View view = inflater.inflate(
                                R.layout.green_lay_inflate_land, null);

                        rel2.addView(view);

                    } else {
                        context = getApplicationContext();
                        LayoutInflater inflater = LayoutInflater.from(context);
                        View view = inflater.inflate(
                                R.layout.green_lay_inflate, null);
                        rel2.addView(view);
                    }
                    rel = (RelativeLayout) findViewById(R.id.Rel_lay_2_p3_n);
                    rel.setVisibility(View.VISIBLE);
                    slide_up = AnimationUtils.loadAnimation(
                            getApplicationContext(), R.anim.go_up);

                    rel2.setVisibility(View.VISIBLE);

                    slide_top = AnimationUtils.loadAnimation(
                            getApplicationContext(), R.anim.go_up);
                    slide_top.setAnimationListener(animationListener_come_up);
                    rel1.setVisibility(View.VISIBLE);
                    rel2.setVisibility(View.INVISIBLE);
                    rel.startAnimation(slide_up);
                    rel1.startAnimation(slide_up);
                    if (i == 1) {
                        LayoutInflater inflater = LayoutInflater.from(context);
                        View view = inflater.inflate(
                                R.layout.green_lay_inflate_land, null);

                        rel2.addView(view);

                    } else {
                        context = getApplicationContext();
                        LayoutInflater inflater = LayoutInflater.from(context);
                        View view = inflater.inflate(
                                R.layout.green_lay_inflate, null);
                        rel2.addView(view);
                    }
                }

            }, 5100);

        }
    };

    private AnimationListener animationListener_come_up = new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {



            b = (Button) findViewById(R.id.b_conti_p3);
            b.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent nextPageIntent = new Intent(getApplicationContext(),
                            P6.class);
                    startActivity(nextPageIntent);

                }
            });

        }
    };
}

MainActivity.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.pal1.ui.P3"
    tools:ignore="MergeRootFrame" 
    android:id="@+id/container2"
      android:background="#E5E4E2">

    <RelativeLayout
        android:id="@+id/Rel_lay_p3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff" >

        <TextView
            android:id="@+id/t_set_conn_p3_n"
            style="@dimen/activity_horizontal_margin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="#4cc417"
            android:gravity="center"
            android:minHeight="30dp"
            android:text="@string/set_conn"
            android:textColor="@android:color/white"
            android:textSize="15sp"
            android:textStyle="bold" />

        <!-- Top Image -->

        <RelativeLayout
            android:id="@+id/Rel_lay_1_p3"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_below="@+id/t_set_conn_p3_n"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:adjustViewBounds="true"
            android:background="#ffffff" >
        </RelativeLayout>

        <!-- Center Image -->

        <ViewFlipper
            android:id="@+id/viewFlipper1"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="52dp"
            android:background="#ffffff" >

            <RelativeLayout
                android:id="@+id/Rel_lay_2_p3"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="#ffffff"
                android:visibility="gone" 
              />

            <RelativeLayout
                android:id="@+id/Rel_lay_2_1_p3"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:background="#ffffff"
                android:visibility="gone" />
        </ViewFlipper>

        <!-- Actual layout -->

        <RelativeLayout
            android:id="@+id/Rel_lay_1_p3_n"
            android:layout_width="match_parent"
            android:layout_height="360dp"
            android:layout_alignParentBottom="true"
            android:layout_below="@+id/Rel_lay_1_p3"
            android:layout_alignParentLeft="true"
            android:background="#E5E4E2" >

            <TextView
                android:id="@+id/t_1_p3_n"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="top"
                android:gravity="top"
                android:padding="5dp"
                android:text="@string/Message_bold_p3"
                android:textColor="#000000"
                android:textSize="18sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/t_2_p3_n"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/t_1_p3_n"
                android:layout_margin="0dp"
                android:gravity="top"
                android:padding="10dp"
                android:text="@string/Message_p3"
                android:textColor="#565051"
                android:textSize="15sp" />
        </RelativeLayout>

        <!-- Invisible Layout -->

        <RelativeLayout
            android:id="@+id/Rel_lay_2_p3_n"
            android:layout_width="match_parent"
            android:layout_height="360dp"
            android:layout_below="@+id/Rel_lay_1_p3"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:background="#E5E4E2"
            android:visibility="invisible" >

            <TextView
                android:id="@+id/t_1_p5_n"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="top"
                android:gravity="top"
                android:padding="5dp"
                android:text="@string/Message_bold_p5"
                android:textColor="#000000"
                android:textSize="18sp"
                android:textStyle="bold" />

            <Button
                android:id="@+id/b_conti_p3"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"
                android:layout_marginBottom="28dp"
                android:background="@drawable/round_button"
                android:ellipsize="none"
                android:text="@string/Button_p5"
                android:textColor="@android:color/white"
                android:clickable="true"
                android:textSize="15sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/t_2_p5_n"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="15dp"
                android:layout_marginRight="15dp"

                android:layout_alignParentLeft="true"
                android:layout_below="@+id/t_1_p5_n"
                android:layout_margin="0dp"
                android:gravity="top"
                android:padding="10dp"
                android:text="@string/Message_p5"
                android:textColor="#565051"
                android:textSize="15sp" />
        </RelativeLayout>
    </RelativeLayout>

</ScrollView>

1 个答案:

答案 0 :(得分:2)

动画结束后为什么要使用按钮点击事件?动画结束后你正在做什么东西?我根据你的代码不这么认为..所以在OnCreate本身使用。我已经通过使用虚拟动画和布局进行了测试并进行了测试。

     setContentView(R.layout.main);
        b = (Button) findViewById(R.id.b_conti_p3);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent nextPageIntent = new Intent(MainActivity.this,
                        NextActivity.class);
                startActivity(nextPageIntent);
                Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT)
                .show();

            }

        });

enter image description here

enter image description here