简体   繁体   English

如何用按钮移动焦点?

[英]How to make the focus move with button?

I made a simple button moving animation. 我制作了一个简单的按钮移动动画。 But after I finish the button translate animation, the button focus did not move with the button. 但是,当我完成按钮转换动画后,按钮焦点没有随按钮一起移动。 The focus still stays in the original position. 焦点仍保持在原始位置。

How to make the focus move with button? 如何用按钮移动焦点? My sdk api level is 15. 我的SDK API等级是15。

The code is like this: 代码是这样的:

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

public class MainActivity extends Activity {

    private Button mButton;

    private Animation mButtonAnimation;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = (Button) findViewById(R.id.button1);
        mButton.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                mButtonAnimation.setFillAfter(true);
                mButton.startAnimation(mButtonAnimation);
            }
        });

        mButtonAnimation = AnimationUtils.loadAnimation(this, R.anim.button_anim);
    }
}

layout: 布局:

<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" >

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

</RelativeLayout>

button_anim.xml: button_anim.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromYDelta="0"
        android:interpolator="@android:anim/linear_interpolator"
        android:toYDelta="100" />
</set>

尝试fillAfter = true ,请参见sdk

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

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