Android 按钮更改背景上的文本
大家好,我是新手,可能需要一些帮助,这很简单 我需要向该代码添加什么Android 按钮更改背景上的文本,android,Android,大家好,我是新手,可能需要一些帮助,这很简单 我需要向该代码添加什么 public class main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setConten
public class main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv2 = (TextView) findViewById(R.id.textView2);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView tx = (TextView) findViewById(R.id.textView2);
tx.setText("Hello");
}
});
}
}
当按下按钮时,它会将文本视图更改为my strings.xml列表中的随机字符串
如果我标记字符串、字符串1、字符串2等
public class main extends Activity {
/** Called when the activity is first created. */
String[] myStrings;
int currentText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv2 = (TextView) findViewById(R.id.textView2);
Button b = (Button) findViewById(R.id.button1);
currentText = 0;
setListeners();
myStrings = new String [200];
myStrings[0] = "String1";
myStrings[1] = "String2";
}
public void changeText(){
tv2.setText(myStrings[currentText[);
currentText++;
}
public void setListeners(){
b = (Button) findViewById(R.id.textView2);
b.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
changeText();
return false;
}
});
}
}
我没有测试这段代码。我希望这对你有帮助 为什么不创建
字符串数组
并按如下方式使用它:
试着这样做:
Resources res=this.getResources();
最终字符串[]大写=res.getStringArray(R.array.list);
按钮下一步=(按钮)findViewById(R.id.button1);
button1.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(你能简单地说出你到底想要达到什么吗?我不能得到你的要求。制作一个应用程序,每当按钮被按下时,它会在屏幕中间给出一个随机的和平文本,当你再次按下它时,它会给你另一个随机文本,等等,因此FordHi,而不是Strug1,StRIG2等等。数组资源?然后你可以简单地使用索引。是的,我可以这样做,我该如何将其放入代码中?好的,现在我已经编辑了我的代码,当你在hello之后再次按下按钮时,我该如何使它成为这样?我收到tv2、b、OnTouchListener的错误,MotionEvent一直说tv2无法解析创建局部变量等等e我是否从字符串中输入文本?将字符串作为字符串数组放入strings.xml
文件中,并在final string[]capital=res.getStringArray(R.array.list);
中分配该数组。
Resources res = this.getResources();
final String[] capital = res.getStringArray(R.array.list);
Button next=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(count<capital.length-1)
tv2.setText(capital[count]);
count++;
}
});