由于我是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函数上方的动画代码将起作用但两个都不会工作我需要两个工作
为我的问题提供解决方案我尝试了很多东西来合并代码,但是有时我没有错误。
答案 0 :(得分:2)
从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();
}});
}