0

我有一个简单的任务问题。

我有 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() atfromAnex.setVisibility(View.VISIBLE); 也在工作,但toAnex.setVisibility(View.GONE);在下面不起作用。

之后,当我触摸按钮可见性时,什么也没有发生。

有人可以看到我的代码有什么问题吗?

对不起我的英语不好,谢谢。

编辑:

这两种方法都不完整,其余的与此按钮可见性问题无关。

当我在创建按钮后将可见性设置为消失时,它可以工作,但是我无法再次将可见性设置为消失,这就是问题所在。

4

1 回答 1

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

}
于 2013-07-17T12:02:37.530 回答