2013-03-02 120 views
1

这里我的问题是如何在点击按钮后显示按钮的新图像,但条件是来自其他类。我是一个新手,我想知道如何连接其他课程的课程。我尝试了意向...如何更改按钮的图像

这里是我的代码

这是我们班的问题...

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

这是问题选择类..

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{ 

    } 

该按钮位于“问题选择”菜单中...

+0

在这个问题..它似乎在我的工作一切都OK ..但我不能连接的意图。我不知道为什么.. – 2013-03-02 13:51:45

回答

0

看来你没有使用包含数据的Bundle。意图只需要下一个活动。套餐必须包括在内。

喜欢:

Bundle b = new Bundle(); 
b.putString("ANSWER", answer); 

Intent intObj = new Intent(Question.this, Level1.class); 
b.putExtras(b); 
startActivity(intObj); 

和隔壁班的。用这个来获得传递的数据:

Bundle b = new Bundle(); 

    b = getIntent().getExtras(); 
    String Gotanswer = b.getString("ANSWER"); 

和使用字符串Gotanswer使用它。

但还有一件事,如果你想字符串答案传递到下一个类时,它会在这个代码是不可能,因为你的每一个状态里面,有它启动下一个类如一条线:

Intent myIntent = new Intent("com.turtleexploration.LEVEL1"); 
     startActivity(myIntent); 

这样做将开始下一节课,而不通过下面的intObj代码。 这是如果我没记错。 :)

plus,而不是使用If-Else-If,switch是这种更好。

当你看到这个答案时,进一步解释你的问题。

Kaya yan pre! haha 
+0

有一个错误的 b.putExtra(b); – 2013-03-02 16:05:10

+0

显示代码。 – ejmtv 2013-03-02 16:13:21

+0

Bundle b = new Bundle(); b.putString(“ANSWER”,answer); Intent intObj = new Intent(Question.this,Level1.class); \t \t b.putExtras(b); \t \t startActivity(intObj); – 2013-03-02 16:36:46