Java字符串转换为相同的文件但输出不同

时间:2014-08-19 13:50:10

标签: java android string

我目前正在学习java,并希望制作一个应用,其中主要活动在main.xmlButton 1 = 1 Button 2 = 2中有两个按钮。

如果您按下我要转到同一页面的任何按钮,但是如果我按下button 1我想要一个文本字段,其中显示1,如果您按button 2,则文本字段显示2 。

我该怎么做?

这是我目前的代码:

第1页(MatchMenuTest):

    public class MatchMenuTest extends Activity implements OnClickListener {

public final static String EXTRA_MESSAGE_LA_HOME = "com.epstudios.basketballmanager_v1.MatchResult";
public final static String EXTRA_MESSAGE_LA_AWAY = "com.epstudios.basketballmanager_v1.MAtchResult";
public final static String EXTRA_MESSAGE_SA_HOME = "com.epstudios.basketballmanager_v1.MATCHRESULT";
public final static String EXTRA_MESSAGE_SA_AWAY = "com.epstudios.basketballmanager_v1.MaTchResult";
Button matchla, matchsa;
String LA, SA;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    matchla.setOnClickListener(this);
    matchsa.setOnClickListener(this);
}

public void matches (){
    String SA = "San Antonio";
    String LA = "Lakers";
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.match1:
        Intent matchresult = new Intent(this, MatchResult.class);
        MatchResult.putExtra(MatchMenuTest.EXTRA_MESSAGE_LA_HOME, LA);
        MatchResult.putExtra(MatchMenuTest.EXTRA_MESSAGE_LA_AWAY, LA);
        startActivity(matchresult);
        break;
    case R.id.match2:
        Intent matchresult1 = new Intent(this, MatchResult.class);
        MatchResult.putExtra(MatchMenuTest.EXTRA_MESSAGE_SA_HOME, SA);
        MatchResult.putExtra(MatchMenuTest.EXTRA_MESSAGE_SA_AWAY, SA);
        startActivity(matchresult1);
        break;
    }
}
}

第2页(匹配):

    public class Match extends Activity implements OnClickListener {

public final static String EXTRA_MESSAGE_HOME = "com.epstudios.basketballmanager_v1.MatchResult";
public final static String EXTRA_MESSAGE_AWAY = "com.epstudios.basketballmanager_v1.MATCHRESULT";
TextView awayGoals, homeGoals, home, away;
Button homebtn, awaybtn, gotoresult;
int homecount, awaycount;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.match);

    baconAndEggs();

    homebtn.setOnClickListener(this);
    awaybtn.setOnClickListener(this);
    gotoresult.setOnClickListener(this);

    String SAAway = getString().getStringExtra(MatchMenuTest.EXTRA_MESSAGE_LA_HOME, -1);
    String LAHome = getString().getStringExtra(MatchMenuTest.EXTRA_MESSAGE_SA_AWAY, -1);
    String SAHome = getString().getStringExtra(MatchMenuTest.EXTRA_MESSAGE_SA_HOME, -1);
    String LAAway = getString().getStringExtra(MatchMenuTest.EXTRA_MESSAGE_LA_AWAY, -1);
}

private void baconAndEggs() {
    awayGoals = (TextView) findViewById(R.id.Away);
    homeGoals = (TextView) findViewById(R.id.Home);
    homebtn = (Button) findViewById(R.id.homeBtn);
    awaybtn = (Button) findViewById(R.id.awayBtn);
    gotoresult = (Button) findViewById(R.id.matchResult);

}

public void counter() {
    awaycount = 1;
    homecount = 0;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.homeBtn:
        homecount++;
        homeGoals.setText("Lakers: " + homecount);
        break;
    case R.id.awayBtn:
        awaycount++;
        awayGoals.setText("Heat: " + awaycount);
        break;
    case R.id.matchResult:
        Intent result = new Intent(this, MatchResult.class);
        result.putExtra(Match.EXTRA_MESSAGE_AWAY, awaycount);
        result.putExtra(Match.EXTRA_MESSAGE_HOME, homecount);
        startActivity(result);
        break;
    }
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}
 }

当我推送matchla时,我想将文本“Lakers:”更改为LA_HOME消息。如果我按下matchsa,文本将变为SA_HOME消息。

1 个答案:

答案 0 :(得分:1)

根据我的理解,你想要这样的东西:

main_activity.xml(PS:我省略了一些宽度和高度设置。但是每个视图都需要它们)

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" >
    <Button android:text="button1" android:id="@+id/button1" />
    <Button android:text="button2" android:id="@+id/button2" />
    <Button android:id="@+id/textview" />
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {
    public void onCreate(Bundle b) {
        setContentView(R.layout.main_activity);
        TextView tv = (TextView)findViewById(R.id.textview);
        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                textview.setText("1");
            }
        });
        findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                textview.setText("2");
            }
        });
    }
}

编辑:考虑到您的要求,尝试实现这段代码:

Intent i = new Intent(this, Match.class);
i.putExtra("home", "lakers");
i.putExtra("away", "san antonio");
startActivity(i);