的EditText输入到阵列(EditText input into array)

2019-10-30 06:54发布

我有一个输入框(EditText1)1,我想继续增加值,并将它们保存到一个数组2.然后我做的时候,我可以点击完成按钮,它可以带我到下一个画面,我可以显示值或调用数组的值..这是我迄今为止1.完成按钮工程2.添加更多按钮,并存储到数组完全不是那么回事....帮助PLZ,

谢谢

编辑:这是编辑的代码:

private EditText txt1;
public static ArrayList<String> playerList = new ArrayList<String>();
String playerlist[];

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen2);

    // edittext1 or textview1
    txt1 = (EditText) findViewById(R.id.editText1);
     final String ag = txt1.getText().toString();



        //add more items button
    Button more = (Button) findViewById(R.id.button1);
    more.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view){
            if(txt1.getText().length() != 0){
                                     playerList.add(ag);
                            DisplayToast("Current Players" + playerList);

            Intent myIntent = new Intent(view.getContext(),
                    Screen2.class);
             myIntent.putExtra("playerList",playerList);
                startActivity(myIntent);

            }

        }});
    }
    //display message 
    private <Strg> String DisplayToast(String ag) {
        Toast.makeText(getBaseContext(), ag, Toast.LENGTH_LONG)
                .show();
        return ag;
    }}

Answer 1:

尝试使你的阵列公共静态的活动,您目前in.Then可以直接访问在接下来的活动,阵列。

编辑:

按照您在您的问题声明的情况下,试试这个代码:说这是你Screen1.class:

private EditText txt1;
public static ArrayList<String> playerList = new ArrayList<String>();
String playerlist[];

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen2);

    // edittext1 or textview1
    txt1 = (EditText) findViewById(R.id.editText1); 

    //add more items button
    Button more = (Button) findViewById(R.id.button1);
    more.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view){

            String ag=txt1.getText().toString().trim();
            if(ag.length() != 0){
                   playerList.add(ag); 
                   txt1.setText(""); // adds text to arraylist and make edittext blank again
            }
        }
    });

    //press Done button to redirect to next activity
    Button done = (Button) findViewById(R.id.done_button);
    done.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view){

            // start new activity
            Intent myIntent = new Intent(view.getContext(),
                    Screen2.class);             
            startActivity(myIntent);

        }
    });
}   

在你Screen2.class,直接访问ArrayList的,如:

String s=Screen1.playerList.get(index);// this gives your the string saved in arraylist at index given.

我希望,它现在很清楚!



Answer 2:

虽然在的onClick完成按钮只是传递的字符串数组,下一个活动并获得下一屏幕数组的值并显示it..You可以通过字符串数组如下

Intent myIntent = new Intent(view.getContext(),
                Screen2.class);

    myIntent.putExtra("Stringarray",arrayvalue);
    startActvity(myIntent);
   finish();

在接下来的活动就可以得到价值

        String[] array=getIntent().getExtras().getStringArray("Stringarray");

编辑:

 for(int i=0;i<array.size;i++)
{
    Toast.makeText(this,array[i], Toast.LENGTH_LONG); // here if you have value in array then it will display in toast one by one.
}

我认为它可以帮助你...



文章来源: EditText input into array