将一个String中的String传递给Android中的另一个Activity

时间:2011-07-15 13:42:49

标签: android

这是我的字符串:

private final String easyPuzzle ="630208010200050089109060030"+
                                 "008006050000187000060500900"+
                                 "09007010681002000502003097";

我想在9 * 9数独板上的另一个活动上显示这个字符串。

7 个答案:

答案 0 :(得分:134)

您需要将其作为额外内容传递:

String easyPuzzle  = "630208010200050089109060030"+
                     "008006050000187000060500900"+
                     "09007010681002000502003097";

Intent i = new Intent(this, ToClass.class);
i.putExtra("epuzzle", easyPuzzle);
startActivity(i); 

然后从您的新活动中提取它:

Intent intent = getIntent();
String easyPuzzle = intent.getExtras().getString("epuzzle");

答案 1 :(得分:16)

在activity1中

    String easyPuzzle  = "630208010200050089109060030"+
                 "008006050000187000060500900"+
                 "09007010681002000502003097";

    Intent i = new Intent (this, activity2.class);

    i.putExtra("puzzle", easyPuzzle);
    startActivity(i);

在activity2中

    Intent i = getIntent();
    String easyPuzzle = i.getStringExtra("puzzle");

答案 2 :(得分:7)

private final String easyPuzzle ="630208010200050089109060030"+
                             "008006050000187000060500900"+
                             "09007010681002000502003097";
Bundle ePzl= new Bundle();
ePzl.putString("key", easyPuzzle);

Intent i = new Intent(MainActivity.this,AnotherActivity.class);
i.putExtras(ePzl);
startActivity(i);

现在转到AnotherActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another_activity);

    Bundle p = getIntent().getExtras();
    String yourPreviousPzl =p.getString("key");

}

现在" yourPreviousPzl"是你想要的字符串。

答案 3 :(得分:3)

发布价值
Intent ii = new Intent(this, GameStartPage.class);

// ii.putExtra("pkgName", B2MAppsPKGName);

ii.putExtra("pkgName", YourValue);
ii.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(ii);

获取价值
pkgn = getIntent().getExtras().getString("pkgName");

答案 4 :(得分:3)

在ActivityOne中,

Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
intent.putExtra("data", somedata);
startActivity(intent);

在ActivityTwo中,

Intent intent = getIntent();
String data = intent.getStringExtra("data");

答案 5 :(得分:1)

最有可能正如其他人所说,您希望将IntentputExtra一起附加到arrayOffsetTop =[]; function getPosition(object) { var list=document.getElementsByClassName("position"); list = [].slice.call(list); alert(list.indexOf(object)); } function changePosition(object) { //alert with the position selected getPosition(object); //store offsetTop of elements in array if(arrayOffsetTop.length==0) { for(var i=0;i<document.getElementsByClassName("position").length;i++) { arrayOffsetTop.push(document.getElementsByClassName("position").item(i).offsetTop); } } //change the elements position document.getElementsByClassName("position").item(2).style.top=arrayOffsetTop[3]; document.getElementsByClassName("position").item(3).style.top=arrayOffsetTop[2]; } 。但我想抛弃那些根据你的用例,最好让一个活动在两个片段之间切换。数据存储在活动中,永远不必传递。

答案 6 :(得分:0)

第一个活动代码:

Intent mIntent = new Intent(ActivityA.this, ActivityB.class);
mIntent.putExtra("easyPuzzle", easyPuzzle);

第二个活动代码:

String easyPuzzle = getIntent().getStringExtra("easyPuzzle");