首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法解决addTextChangedListener问题

无法解决addTextChangedListener问题
EN

Stack Overflow用户
提问于 2016-04-21 12:13:50
回答 1查看 403关注 0票数 0

我在创建一个允许添加文本框并将其从屏幕上删除的应用程序时出错了。我遇到的错误涉及到addTextChangedListener方法,因为Android说它不能解决这个方法。我知道这个方法必须放在onCreate方法中,但是我不知道如何修改我的代码来实现这一点。任何帮助都会是很棒的。我的主要代码如下。

代码语言:javascript
复制
package com.example.a11111111.gamesaid;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    //Parent view for the main screen
    private LinearLayout layoutView;

    //Add Button
    private Button addButton;

    //One empty row must always be present
    private View emptyView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        layoutView = (LinearLayout) findViewById(R.id.parentView);
        addButton = (Button) findViewById(R.id.addButton);


    }

    //create on click handler for the add button

    public void addNew(View v)
    {
        addRow(null, 0);
        v.setVisibility(View.GONE);
    }

    //Add delete handler

    public void deleteRow(View v)
    {
        layoutView.removeView((View) v.getParent());
    }

    public void addRow(String name, int score) {

        LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View row = inflate.inflate(R.layout.activity_main, null);
        final Button delete = (Button) row.findViewById(R.id.buttonDelete);
        final EditText editName = (EditText) row.findViewById(R.id.name);
        final EditText editScore = (EditText) row.findViewById(R.id.score);


        //if a value is entered in both fields, add it to the activity
        //else delete the row
        if (name != null && !name.isEmpty() && score != 0 )
        {
            editName.setText(name);
            editScore.setText(score);
        } else

        {
            emptyView = row;
            delete.setVisibility(View.INVISIBLE);
        }


        //add a textWatcher to control visibility of Add button and empty view

        name.addTextChangedListener(new TextWatcher()
        {
             @Override
             public void afterTextChanged(Editable i) {
                 if (i.toString().isEmpty()) {
                     addButton.setVisibility(View.GONE);
                     delete.setVisibility(View.INVISIBLE);


                     if (emptyView != null && emptyView != row) {
                         layoutView.removeView(emptyView);
                     }
                     emptyView = row;
                 } else {
                     if (emptyView == row) {
                         emptyView = null;
                     }

                     addButton.setVisibility(View.VISIBLE);
                     delete.setVisibility(View.VISIBLE);
                 }
             }

             @Override
             public void beforeTextChanged(CharSequence s, int start, int count,int after)

             {
             }

             @Override
             public void onTextChanged(CharSequence s, int start, int before,int count)
             {
             }
        });

        layoutView.addView(row, layoutView.getChildCount()- 1);


    }










    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-21 12:31:48

变量name的类型为StringString没有addTextChangedListener()方法。

我认为您应该将侦听器添加到编辑文本中,很可能是editName

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36769273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档