繁体   English   中英

Android按钮中的onclick函数与动画不起作用

[英]onclick function in android button with animation is not working

由于我是android的新手,我编写了一个代码,其中包含一个按钮,该按钮必须动画并链接到另一个xml文件。 但是当我编译它时,按钮会动画,但它不会链接到另一个文件。 当我点击它时,我需要动画按钮进入另一个xml页面。

代码和图像显示如下

button.xml

<RelativeLayout 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"
android:background="@drawable/bulbasaur"
tools:context=".MainActivity" >
<Button
android:id="@+id/enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="58dp"
android:onClick="onEnterClicked"
android:text="@string/enter_button" />
</RelativeLayout>

/res/anim/anim_alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<alpha
   android:fromAlpha="1.0"
   android:toAlpha="0.1"
   android:duration="500"
   android:repeatCount="1"
   android:repeatMode="reverse" />
</set>

MainActivity.java

package com.coded.sandeep;
import com.coded.sandeep.SecondActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Animation animAlpha2 = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);

Button btnEnter = (Button)findViewById(R.id.enter);

btnEnter.setOnClickListener(new Button.OnClickListener(){

      @Override
      public void onClick(View arg1) {
       arg1.startAnimation(animAlpha2); //when i click the button animation is working 
                                          but onEnterClicked is not working can anyone 
                                                  help me to edit the code here such that both 
                                                  animation and link to another xml page works        
      }});  }
public void onEnterClicked(View view) 
{
    startActivity(new Intent(getApplication(),SecondActivity.class));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;  }}

朋友可以帮助我如何包括onEnterclicked和动画,以便按钮将动画,然后转到我已在secondActivity.java中声明的其他xml文件(在程序中声明)

如果我删除onenterclicked函数上面的动画代码将工作,但两个都不会工作我需要两个工作

给我的问题解决方案我尝试了很多东西来合并代码,但它不工作有时我会得到错误。

从xml文件中的按钮中删除android:onClick="onEnterClicked"

  btnEnter.setOnClickListener(new Button.OnClickListener(){

           @Override
              public void onClick(View arg1) {
               arg1.startAnimation(animAlpha2); 

Thread thread = new Thread() {
            @Override
            public void run() {
                // Block this thread for 2 seconds.
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                }

                // After sleep finished blocking, create a Runnable to run on
                // the UI Thread.
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                      startActivity(new Intent(getApplication(),SecondActivity.class)); 
                    }

                });
            }
        };

        // Don't forget to start the thread.
        thread.start();



              }}); 
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM