单击按钮时显示和隐藏EditText

时间:2014-05-01 16:56:11

标签: android android-animation android-imagebutton

我有ImageButton使EditText可见:

final ImageButton k = (ImageButton) findViewById(R.id.imageSearchButton);
k.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        Animation anim = AnimationUtils.loadAnimation(CallActivity.this, R.anim.search_jump);
        k.startAnimation(anim);

        mySearchView = (EditText) findViewById(R.id.searchText);
        mySearchView.setVisibility(View.VISIBLE);
        Animation searchAnim = AnimationUtils.loadAnimation(CallActivity.this, R.anim.push_left_in);
        mySearchView.startAnimation(searchAnim);

    }
});

点击ImageButton后,EditText从右向左滑动,然后再次点击ImageButton时,EditText应再次滑出,并隐藏它,我怎么能创造这种效果?

2 个答案:

答案 0 :(得分:3)

您可以通过检查EditText是否可见来执行此操作:

if(mySearchView.getVisibility() == View.VISIBLE){
    mySearchView.setVisibility(View.GONE);
} else {
    mySearchView.setVisibility(View.VISIBLE);
}

答案 1 :(得分:1)

您可以像这样使用ToggleButton

ToggleButton b = (ToggleButton) findViewById(R.id.myButton);

// attach an OnClickListener
b.setOnClickListener(new OnClickListener()
{
    if(((ToggleButton) view).isChecked()) {
    // handle toggle on
} else {
   // handle toggle off
} 
});

并将图像设置为togglebuttons的背景