簡體   English   中英

如何用按鈕移動焦點?

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

我制作了一個簡單的按鈕移動動畫。 但是,當我完成按鈕轉換動畫后,按鈕焦點沒有隨按鈕一起移動。 焦點仍保持在原始位置。

如何用按鈕移動焦點? 我的SDK API等級是15。

代碼是這樣的:

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);
    }
}

布局:

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

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