【问题标题】:Edit text expand on click编辑文本点击展开
【发布时间】:2018-06-13 23:02:21
【问题描述】:

我有一个编辑文本,在键入时需要扩展到应用程序的整个宽度,当在外部或其他任何地方单击时它会恢复到正常大小。 我找到了一段代码,它确实可以满足我的需求,但它并不成立,例如一旦代码结束运行,它就会反转过程。我正在寻找解决此问题的方法。或者另一种方法来做到这一点,与此类似的示例是在移动设备上使用网络浏览器时,当输入 url 或搜索词时,url 栏会扩展为全宽。

家庭活动

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.EditText;

 public class Home extends AppCompatActivity {

public EditText search_bar;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);


   search_bar = (EditText) findViewById(R.id.search_bar);

    }
    public void edit_clicked(View view){

    Animation scaleAnimation = new ScaleAnimation(0, 2, 1, 2);
    scaleAnimation.setDuration(750);
    search_bar.startAnimation(scaleAnimation);
    }

}

【问题讨论】:

  • 你能不能解释的更清楚一点?您希望在插入文本后将编辑文本调整回初始大小还是什么?

标签: android animation web webview android-edittext


【解决方案1】:

这样做的动态方式:

   ScaleAnimation animate = new ScaleAnimation(1, 2, 1, 1, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
    animate.setDuration(700);
    animate.setFillAfter(true);
    target.startAnimation(animate);

使用 XML 的方式

scale.xml(把这个放在 res/anim 文件夹中)

    <scale
        android:duration="400"
        android:fillAfter="true"        
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="0%"
        android:pivotY="0%"
        android:toXScale="2.0"
        android:toYScale="1.0" >
    </scale>

</set>

Java 代码

Animation anim = AnimationUtils.loadAnimation(Anims.this, R.anim.scale);
mEdittext.startAnimation(anim);

(或)

简单试试这个

Animation scaleAnimation = new ScaleAnimation(0, -500, 1, 1);
    scaleAnimation.setDuration(750);
    scaleAnimation.setFillAfter(true);
    editText.startAnimation(scaleAnimation);

【讨论】:

  • 谢谢你,这工作得很好,有没有办法在不使用它时撤消的编辑文本
  • Animation 动画,完美运行,但是当 Edittext 之外的任何地方被触摸或点击时,代码会是什么?
猜你喜欢
  • 1970-01-01
  • 2014-11-17
  • 1970-01-01
  • 2011-09-17
  • 1970-01-01
  • 1970-01-01
  • 2020-03-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多