当editText位于另一个onclick内时,如何在editText字段上使用getText()?

时间:2018-07-29 11:03:37

标签: java android onclicklistener gettext

当前,我的应用程序存在的问题是,当通过浮动操作按钮onClickListener切换我的应用程序视图时,我无法访问放置在我的editText中的内容,因为它是在单独的for循环中创建的。


start_button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
for(int j=0;j<amount;j++){

                ...
                final  EditText team = new EditText(MainActivity.this);
                team.setHint("TeamName");
                team.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
                ll.addView(team);

                ...
                });

                lm.addView(ll);
            }
            fab2.setVisibility(View.VISIBLE);
                fab2.setOnClickListener(new OnClickListener() {
                    public void onClick(View view) {


        if(lm.getVisibility()==View.GONE|lm2.getVisibility()==View.VISIBLE) {
                            lm2.setVisibility(View.GONE);
                            lm2.removeAllViews();
                            lm.setVisibility(View.VISIBLE);
                        }else{
         if(lm.getVisibility()==View.VISIBLE|lm2.getVisibility()==View.GONE){
                       lm.setVisibility(View.GONE);

                       for(int x=0;x<amount;x++){
                               if(myStringArray[x]==null){
                                   myStringArray[x]="Empty Team Name";
                                   x--;
                               }else {
                                   myStringArray[x] = (team.getText().toString());
                                   TextView score_name = new TextView(MainActivity.this);
                                   TextView final_score = new TextView(MainActivity.this);
                                   score_name.setTextSize(15);
                                   score_name.setTypeface(null, Typeface.BOLD);
                                   final_score.setTextSize(25);
                                   score_name.setLayoutParams(params);
                                   final_score.setLayoutParams(params);
                                   lm2.addView(score_name);
                                   lm2.addView(final_score);
                                   String a = Arrays.toString(myIntArray);
                                   String ar[] = a.substring(1, a.length() - 1).split(", ");

                                   score_name.append(myStringArray[x]);
                                   final_score.append(ar[x]);
                                   score_name.append("\n");
                               }
                            }
                            lm2.setVisibility(View.VISIBLE);
                        }
                    }
                });

当前在使用时无法识别变量(团队)

myStringArray [x] =(team.getText()。toString());

我曾尝试移动初始化器{{final EditText team = new EditText(MainActivity.this);}},但是如果我将其移动到for循环外,则在单击start_button时应用程序将崩溃

1 个答案:

答案 0 :(得分:0)

start_button.setOnClickListener(new OnClickListener()的站点之外定义EditText团队,然后使用onClickListener方法对其进行初始化。检查下面的代码。

final  EditText team;
start_button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
for(int j=0;j<amount;j++){

                team = new EditText(MainActivity.this);
                team.setHint("TeamName");
                team.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
                ll.addView(team);

                ...
                });

                lm.addView(ll);
            }
            fab2.setVisibility(View.VISIBLE);
                fab2.setOnClickListener(new OnClickListener() {
                    public void onClick(View view) {


        if(lm.getVisibility()==View.GONE|lm2.getVisibility()==View.VISIBLE) {
                            lm2.setVisibility(View.GONE);
                            lm2.removeAllViews();
                            lm.setVisibility(View.VISIBLE);
                        }else{
         if(lm.getVisibility()==View.VISIBLE|lm2.getVisibility()==View.GONE){
                       lm.setVisibility(View.GONE);

                       for(int x=0;x<amount;x++){
                               if(myStringArray[x]==null){
                                   myStringArray[x]="Empty Team Name";
                                   x--;
                               }else {
                                   myStringArray[x] = (team.getText().toString());
                                   TextView score_name = new TextView(MainActivity.this);
                                   TextView final_score = new TextView(MainActivity.this);
                                   score_name.setTextSize(15);
                                   score_name.setTypeface(null, Typeface.BOLD);
                                   final_score.setTextSize(25);
                                   score_name.setLayoutParams(params);
                                   final_score.setLayoutParams(params);
                                   lm2.addView(score_name);
                                   lm2.addView(final_score);
                                   String a = Arrays.toString(myIntArray);
                                   String ar[] = a.substring(1, a.length() - 1).split(", ");

                                   score_name.append(myStringArray[x]);
                                   final_score.append(ar[x]);
                                   score_name.append("\n");
                               }
                            }
                            lm2.setVisibility(View.VISIBLE);
                        }
                    }
                });