如何用另一个FAB切换FAB的可见性

时间:2017-05-22 20:58:26

标签: java android kotlin

此代码使按下的FAB消失:

val fab = findViewById(R.id.fab) as FloatingActionButton
    fab.setOnClickListener({ view ->
        findViewById(R.id.fab2)

        if(view.visibility == VISIBLE) {
            view.visibility = INVISIBLE
        }
        else view.visibility = VISIBLE

2 个答案:

答案 0 :(得分:0)

findViewById(R.id.fab1).setOnClickListener {
    val fab2 = findViewById(R.id.fab2)

    fab2.visibility = if(fab2.visibility == View.VISIBLE) View.INVISIBLE else View.VISIBLE
}

findViewById(R.id.fab1).setOnClickListener { view ->
    val fab2 = findViewById(R.id.fab2)

    fab2.visibility = if(fab2.visibility == View.VISIBLE) View.INVISIBLE else View.VISIBLE
}

findViewById(R.id.fab1).setOnClickListener {
    findViewById(R.id.fab2).apply { 
        visibility = if(visibility == View.VISIBLE) View.INVISIBLE else View.VISIBLE
    }
}

答案 1 :(得分:0)

fab1=(FloatingActionButton)findViewById(R.id.fab1);
fab2=(FloatingActionButton)findViewById(R.id.fab2);
fab1.setOnClickListener(new View.OnClickListener{
public void onClick(View view){
if(fab2.getVisibility()==View.VISIBLE)
fab.setVisibility(View.GONE)or invisible
else
fab.setVisibility(View.VISIBLE)

}});