的EditText和使用按钮来提交他们 [英] EditText and using buttons to submit them

查看:88
本文介绍了的EditText和使用按钮来提交他们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我想这允许用户输入文本(自己的名字)。然后点击提交按钮,将这个名字存入数组,并删除了的EditText写作(以及让玩家计数器)。他们这样做的球员提交仍照我希望他们能够点击播放按钮(标题为DONE我相信),并继续到下一个页面与所发送的所有信息。

我的问题目前是,当我在名称并点击提交强制关闭。如果我点击播放按钮,它强制关闭。你觉得你能帮助我吗?谢谢

第1类:

 公共类的Class1扩展活动
{
INT玩家= 0;
字符串名称[];公共无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.class1);    最终的EditText输入=(EditText上)findViewById(R.id.nameinput);
    按钮提交按钮=(按钮)findViewById(R.id.submit_btn);
    submitButton.setOnClickListener(新View.OnClickListener()
    {
        公共无效的onClick(查看submit1)
        {
            玩家++;
            的for(int i = 0; I<球员;我++)
            {
                。名字[I] = input.getText()的toString();
                input.setText();
            }
        }
    });    按钮doneButton =(按钮)findViewById(R.id.done_btn);
    doneButton.setOnClickListener(新View.OnClickListener()
    {
        公共无效的onClick(查看DONE1)
        {
            意图做=新意图(Class1.this,Class2.class);
            done.putExtra(选手中,玩家);
            done.putExtra(名,名称[玩家]);
            startActivity(完成);
        }
    });
}


解决方案

你不分配变量名称的任何地方。你应该这样做:

  =名新的String [NO_OF_PLAYERS]

如果你不知道 NO_OF_PLAYERS ,然后用

 的ArrayList<串GT;名称=新的ArrayList<串GT;();

和使用它:

  names.add(input.getText()的toString());

So basically I would like this to allow the user to input text (their name). Then to click the submit button which will store that name into an array and erase the writing in the EditText (as well as make a counter for players). After they are done submitting players names I want them to be able to click the play button (titled done i believe) and continue to the next page with all the information being sent over.

My problem currently is when i enter in a name and click submit it force closes. If i click the play button it force closes. Think you could help me out? Thanks

Class 1:

public class Class1 extends Activity
{  
int players=0;
String names[];

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.class1);

    final EditText input = (EditText) findViewById(R.id.nameinput);


    Button submitButton = (Button) findViewById(R.id.submit_btn);
    submitButton.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View submit1)
        {
            players++;
            for(int i=0; i < players; i++)
            {
                names[i] = input.getText().toString();
                input.setText("");
            }
        }
    });

    Button doneButton = (Button) findViewById(R.id.done_btn);
    doneButton.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View done1)
        {
            Intent done = new Intent(Class1.this, Class2.class);
            done.putExtra("players", players);
            done.putExtra("names", names[players]);
            startActivity(done);
        }
    });
}

解决方案

You're not allocating for variable names anywhere. You should do this:

names = new String[NO_OF_PLAYERS];

If you're not sure about NO_OF_PLAYERS, then use

ArrayList<String> names = new ArrayList<String>();

and use it:

names.add(input.getText().toString());

这篇关于的EditText和使用按钮来提交他们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆