2

我目前已经设置好了,所以有几个 TextViews:

  • 单击一个按钮时其中一个会更新,单击另一个按钮时不会更改它。

  • 其他 TextViews 显示一个数字,我希望在单击任一按钮时更改它们,但目前所有 TextViews 都是可见的并且数字没有改变。

我希望其他 TextViews(下面的 num1-num3)最初是不可见的,然后当用户单击其中一个按钮时,TextViews 变得可见,并且它们的值由我编写的方法更新。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_name);
    num1 = randNum();
    num1 = alterNum(num1);      
    num1View = (TextView) findViewById(R.id.number1); 
    num1View.setText("Num1 Number: " + String.valueOf(num1));

    num2 = randNum();
    num2 = alterNum(num2);
    num2View = (TextView) findViewById(R.id.number2); 
    num2View.setText("Num2 Number: " + String.valueOf(num2));

    num3 = randNum();
    num3 = alterNum(num3);
    num3View = (TextView) findViewById(R.id.number3); 
    num3View.setText("Num3 Number: " + String.valueOf(num3));

    // This one is always visible, the ones above should be invisible
    // and appear onClick
    currentNum = randNum();
    myTextView = (TextView) findViewById(R.id.current_number); 
    myTextView.setText("Current Number: " + String.valueOf(currentNum));
    okButton = (Button) findViewById(R.id.ok_button);
    okButton.setOnClickListener(this);
    changeButton = (Button) findViewById(R.id.change_button);
    changeButton.setOnClickListener(this);
}

我的点击:

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.num_confirmation:
        //do nothing
        // do not let user hit buttons more than once (either case)
        changeButton.setEnabled(false);
        okButton.setEnabled(false);
        break;
    case R.id.swap_button:
        currentNum = alterNum();
        myTextView.setText("Current Number: " + String.valueOf(currentNum));
        // do not let user hit buttons more than once (either case)
        swapButton.setEnabled(false);
        okButton.setEnabled(false);
        break;
    default:
        break;
    }
}

这是怎么做到的?

4

2 回答 2

6

set the initial value of "visible" to "invisible" in your xml layout file for textViews that should be invisible and in your onClick method alter its text valie and change visibility:

 myTextView.setVisibility(View.VISIBLE);

here is similar question that should help: How to change visibility of layout programaticly

于 2012-12-09T19:16:01.147 回答
2
yourTextView.setVisibility(View.VISIBLE);

这使您的文本视图可见。

yourTextView.setVisibility(View.INVISIBLE);

这使您的 textview 不可见,但保留布局。

yourTextView.setVisibility(View.GONE);

This removes it so other Views can rearrange. You can still call View.VISIBLE to make it appear again.

So for example you could put this after you define the TextView (findviewbyid)

 num1View.setVisibility(View.INVISIBLE);

Then where you want it to come back:

num1View.setVisibility(View.VISIBLE); 
于 2012-12-09T19:14:54.983 回答