1

以下是我的主要活动文件。我被底部区域困住了(注释掉了)。我想这样做,以便当我点击 button 时b1,文本会从默认值更改。所以说它默认说“你好”,当你按下按钮时,我想将文本切换为“你好吗” b1。我尝试了很多东西,但总是出错。有人能帮我吗?

package com.wdmc85.donottouch;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class DoNotTouchActivity extends Activity 
implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button b1 = (Button) this.findViewById(R.id.b1);
    b1.setOnClickListener(this);


}

@Override
public void onClick(View v) 
{
    switch(v.getId())
    {
    case R.id.b1:
        //WHAT DO I PUT IN HERE?? I WANT IT SO WHEN YOU HIT BUTTON B1, THE 
        //TEXT CHANGES FROM WHAT IT IS FOR DEFAULT. SO SAY IT SAYS "HELLO"
        //AS DEFAULT, HOW DO I MAKE IT SO WHEN YOU PRESS BUTTON B1 THE TEXT
        //CHANGES FROM "HELLO" TO "HOW ARE YOU?" I HAVE TRIED ALL SORTS OF
        //THINGS BUT NOTHING HAS WORKED
    break;
    }

}

}

4

2 回答 2

0

您应该使用以下方法来更改文本视图的文本。

 textView.setText("your text");

参考链接是>> textview>>setText()

可以对按钮和其他视图使用相同的。

于 2012-06-30T18:26:29.217 回答
0

如果要更改按钮本身的文本

 switch(v.getId())
        {
        case R.id.b1:
           Button b1 = (Button) v;
           b1.setText("HOW ARE YOU?");

        break;
        }

如果要更改任何其他 Textview 的文本

 switch(v.getId())
        {
        case R.id.b1:
           TextView tv = (TextView ) this.findViewById(R.id.<tvid>);
           tv .setText("HOW ARE YOU?");

        break;
        }
于 2012-06-30T18:34:54.250 回答