0

我目前正在制作一个具有edittext和2个按钮的应用程序,每次我在edittext中写一些东西然后按button1,都会添加一个新的textview。这是代码:

public void novVnos (View v){
    EditText eText = (EditText) findViewById(R.id.editText1);
    LinearLayout mLayout = (LinearLayout) findViewById(R.id.linearLayout);

    mLayout.addView(createNewTextView(eText.getText().toString()));
}

private TextView createNewTextView (String text){
    final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    final TextView newTextView = new TextView(this);

    newTextView.setLayoutParams(lparams);
    newTextView.setText(text);
    return newTextView;
}

基本上我正在做的是添加新的“玩家”,一旦我添加了他们所有我想按下按钮2。Button2 打开一个新活动,新活动应该读取我在上一个活动中添加的所有文本视图。

4

3 回答 3

1

我要做的是创建一个“播放器”classstatic ArrayList<String> players在您的Player.java班级中创建一个。每次调用时,createNewTextView(textView)将任何变量添加textplayers ArrayList.

在您的下一个课程中,Activity您只需调用Player.players.get(index)或任何ArrayList您需要的功能,以便在下一节课中使用它做任何您想做的工作。你也可以ArrayList在你的 current 中创建Activity它并将它作为额外的传递,Intent但我认为为玩家创建一个单独的类是最简单的。

ArrayList显然不需要持有Strings。它可以容纳任何你想要的东西,包括一个 Player 对象。

数组列表文档

于 2013-07-01T22:37:12.413 回答
0

实现“播放器”类将是一种方法。在这方面,您不必担心在两个活动之间传输数据。您可以使用 Singleton 方法来确保您的类只定义了一次。您需要确保它只定义一次,因为假设您的用户关闭应用程序或杀死应用程序,然后当他再次打开应用程序时,它将创建另一个类,因此您的所有数据都将丢失。

于 2013-07-02T03:39:49.267 回答
0

这是一个例子:

在第一个活动中:

   Button search = (Button) findViewById(R.id.Button02);
    search.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {


            Intent intent = new Intent(MainActivity.this, result.class);
            Bundle b = new Bundle();

            EditText searchtext = (EditText) findViewById(R.id.searchtext);

            b.putString("searchtext", searchtext.getText().toString());
                   //Add the set of extended data to the intent and start it
            intent.putExtras(b);
            startActivityForResult(intent,RESULT_ACTIVITY);
        }
    });

在 onCreat 的第二个活动中:

 Bundle b = getIntent().getExtras();
    String searchtext = b.getString("searchtext"); //here you get data then use it as you want

  //here I use it to show data in another edittext
     EditText etSearch = (EditText) findViewById(R.id.searchtext2);
     etSearch.setText(searchtext);
于 2013-07-01T23:14:26.250 回答