如何改变一个按钮的图像 [英] How to change the image of a button
问题描述
在这里我的问题是如何显示按钮,一个新的形象获得了点击,但条件是从其他下课。在这里我是一个新手,我想知道如何在类与其他类连接。我试过意图...
My problem here is how to display a new image for button after it was clicked but the condition is from the other class. Im a newbie here and I want to know how to connect the class from other class. I tried the Intent ...
下面是我的code
这是我们班的问题...
This is the class of our Question...
@Override
// TODO Auto-generated method stub
public void onClick(View v) {
String answer = "Marianas Trench";
String answer2 = "marianas trench";
String answer3 = "MARIANAS TRENCH";
String check = input.getText().toString();
if (check.contentEquals(answer)){
tvresult.setText("Correct");
Intent myIntent= new Intent("com.turtleexploration.LEVEL1");
startActivity(myIntent);
}else if (check.contentEquals(answer2)){
tvresult.setText("Correct");
Intent myIntent= new Intent("com.turtleexploration.LEVEL1");
startActivity(myIntent);
}else if (check.contentEquals(answer3)){
tvresult.setText("Correct");
Intent myIntent = new Intent("com.turtleexploration.LEVEL1");
startActivity(myIntent);
}else{
tvresult.setText("Wrong");
}
Intent intObj = new Intent(Question.this, Level1.class);
intObj.putExtra("ANSWER", answer);
startActivity(intObj);
}
这是问题的Selection类..
And this is the Question Selection class..
ImageButton l1 = (ImageButton) findViewById(R.id.l1);
TextView t1 = (TextView) findViewById(R.id.t1);
Intent intename = getIntent();
String mainans = "Marianas Trench";
String ans = (String) intename.getSerializableExtra("ANSWER");
if (ans == mainans){
l1.getBackground().equals(getResources().getDrawable(R.drawable.m1));
t1.setText("Correct");
}else{
}
按钮是在问题的选择菜单...
The button is in the Question Selection menu...
推荐答案
看来你没有使用它携带的数据包。意图只要求对下一个活动。包必须被包括在内。
it seems that you did not use the Bundle which carries the data. Intent only calls for the next activity. Bundle must be included.
这样的:
Bundle b = new Bundle();
b.putString("ANSWER", answer);
Intent intObj = new Intent(Question.this, Level1.class);
b.putExtras(b);
startActivity(intObj);
和隔壁班的。用这个来获得数据传递:
and in the next class. use this to get the data passed:
Bundle b = new Bundle();
b = getIntent().getExtras();
String Gotanswer = b.getString("ANSWER");
和使用字符串 Gotanswer
来使用它。
and use the string Gotanswer
to use it.
但有一件事,如果你想在字符串答案传递给下一个班,这将是在这个code不可能的,因为你的每一个状态里面,有启动下一类如行:
BUT one more thing, if you wanna pass the string ANSWER to the next class, it would be impossible in this code because inside your every condition, there is a line that starts the next class such as:
Intent myIntent = new Intent("com.turtleexploration.LEVEL1");
startActivity(myIntent);
这样做会开始下一课,而不会通过 intObj
$ C $传递C以下。
这是,如果我没有记错。 :)
doing this will start the next class without passing through the intObj
code below.
this is if I'm not mistaken. :)
加,而不是使用的if-else-如果
,开关
是多的这种好。
plus, instead of using If-Else-If
, switch
is much better for this kind.
进一步解释您的问题,当你看到这个答案。
Explain your issue further when you see this answer.
Kaya yan pre! haha
这篇关于如何改变一个按钮的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!