EditText上输入数组 [英] EditText input into array
问题描述
我有一个输入框(EditText1)
1.我想保持增加值,并将其保存到一个数组
2.然后我做的时候,我可以点击完成按钮,它可以带我到下一个画面,我可以显示该值或致电数组的值..这是我迄今为止
1.完成按钮厂
2.添加更多按钮和存储数组完全不是那么回事....帮助PLZ,
感谢
编辑:
下面是编辑code:
私人的EditText TXT1;
公共静态的ArrayList<串GT; playerList =新的ArrayList<串GT;();
串playerlist [];/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.screen2); // edittext1或textview1
TXT1 =(EditText上)findViewById(R.id.editText1);
。最后弦乐AG = txt1.getText()的toString(); //添加更多的商品按钮
按钮更多=(按钮)findViewById(R.id.button1);
more.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(查看视图){
如果(txt1.getText()。长度()!= 0){
playerList.add(AG);
DisplayToast(当前的运动员+ playerList); 意图myIntent =新意图(view.getContext()
Screen2.class);
myIntent.putExtra(playerList,playerList);
startActivity(myIntent); } }});
}
//显示消息
私人<&STRG GT;字符串DisplayToast(字符串AG){
Toast.makeText(getBaseContext(),AG,Toast.LENGTH_LONG)
。显示();
返回股份公司;
}}
试着做阵列公共静态的活动,您目前in.Then可以直接访问该数组中下一个活动。
编辑:
根据你在你的问题中声明的情况下,试试这个code:
说这是你Screen1.class:
私人的EditText TXT1;
公共静态的ArrayList<串GT; playerList =新的ArrayList<串GT;();
串playerlist [];@覆盖
公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState);
的setContentView(R.layout.screen2); // edittext1或textview1
TXT1 =(EditText上)findViewById(R.id.editText1); //添加更多的商品按钮
按钮更多=(按钮)findViewById(R.id.button1);
more.setOnClickListener(新View.OnClickListener(){ 公共无效的onClick(查看视图){ 字符串AG = txt1.getText()的toString()修剪()。
如果(ag.length()!= 0){
playerList.add(AG);
txt1.setText(); //将文本添加到ArrayList和再犯的EditText空白
}
}
}); // preSS完成按钮重定向到下一个活动
按钮来完成=(按钮)findViewById(R.id.done_button);
done.setOnClickListener(新View.OnClickListener(){ 公共无效的onClick(查看视图){ //启动新的活动
意图myIntent =新意图(view.getContext()
Screen2.class);
startActivity(myIntent); }
});
}
在您Screen2.class,直接访问的ArrayList这样的:
一个String = Screen1.playerList.get(指数); //这给你在给定的索引保存在ArrayList中的字符串。
我希望,它现在是清楚了!
I have an input box (EditText1) 1. that i want to keep adding values and save them to an Array 2. then when am done, i can click the done button and it can take me to the next screen and I can display the values or call the values of the array.. this is what i have so far 1. Done Button Works 2. Add more button and storing to array doesn't quite work....HELP PLZ,
Thanks
EDIT : Here is the edited code:
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;
}}
Try making your array public static in the activity you are currently in.Then you can directly access that array in next activity.
EDIT :
As per the scenario you declared in your question,Try this code: say this is your 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);
}
});
}
In your Screen2.class,directly access ArrayList like:
String s=Screen1.playerList.get(index);// this gives your the string saved in arraylist at index given.
I hope,it's clear now!
这篇关于EditText上输入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!