我遇到一个简单的任务问题。
我有2个按钮,我希望同时只能看到一个,所以当你触摸一个时它会隐藏而另一个会出现。
这是我的代码:
fromAnex = new Button(this);
fromAnex.setText("from");
fromAnex.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("ONClickListener", "from anex");
returnFromAnex();
}
});
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.LEFT_OF,plano.getId());
params.addRule(RelativeLayout.CENTER_VERTICAL);
rl.addView(fromAnex,params);
fromAnex.setVisibility(View.GONE);
toAnex = new Button(this);
toAnex.setText("to");
toAnex.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("ONClickListener", "Show anex");
showAnex();
}
});
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.RIGHT_OF,plano.getId());
params2.addRule(RelativeLayout.CENTER_VERTICAL);
rl.addView(toAnex,params2);
private void showAnex()
{
fromAnex.setVisibility(View.VISIBLE);
toAnex.setVisibility(View.GONE);
private void returnFromAnex()
{
fromAnex.setVisibility(View.GONE);
toAnex.setVisibility(View.VISIBLE);
我真的不明白为什么,但第一次拨打fromAnex.setVisibility(View.GONE);
的工作正如预期的那样,第一次调用showAnex()fromAnex.setVisibility(View.VISIBLE);
也在工作,但toAnex.setVisibility(View.GONE);
正在工作。
之后触摸时按钮可见性没有任何反应。
有人可以看到我的代码有什么问题吗?
抱歉我的英文不好,谢谢。
修改
这两种方法都没有完成,其余的与这个按钮可见性问题无关。
当我在创建按钮之后将可见性设置为已消失时,它可以正常工作,但是我无法将可见性设置为再次出现,这就是问题所在。
答案 0 :(得分:0)
拿你的片段我尝试运行以下内容并且它运行正常。我猜你在DevTest所说的某些小错误。
public class MainActivity extends Activity {
Button fromAnex,toAnex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rl=new RelativeLayout(getApplicationContext());
RelativeLayout.LayoutParams relPra=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
rl.setLayoutParams(relPra);
fromAnex = new Button(this);
fromAnex.setText("from");
fromAnex.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("ONClickListener", "from anex");
returnFromAnex();
}
});
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
rl.addView(fromAnex,params);
fromAnex.setVisibility(View.GONE);
fromAnex.setId(1);
toAnex = new Button(this);
toAnex.setId(2);
toAnex.setText("to");
toAnex.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("ONClickListener", "Show anex");
showAnex();
}
});
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.RIGHT_OF,fromAnex.getId());
params2.addRule(RelativeLayout.CENTER_VERTICAL);
rl.addView(toAnex,params2);
setContentView(rl);
}
private void showAnex()
{
fromAnex.setVisibility(View.VISIBLE);
toAnex.setVisibility(View.GONE);
}
private void returnFromAnex()
{
fromAnex.setVisibility(View.GONE);
toAnex.setVisibility(View.VISIBLE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}