addTextChangedListener无法解析

时间:2016-04-21 12:13:50

标签: java android-studio

我尝试创建一个允许添加文本框并从屏幕上删除的应用程序时出错。我遇到的错误涉及addTextChangedListener方法,因为Android Studio说它无法解析方法。我知道这个方法必须放在onCreate方法中,但我不确定如何改变我的代码来实现这个目的。任何帮助都会非常棒。我的主要代码如下。

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);
    }
}

1 个答案:

答案 0 :(得分:0)

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

我想你应该将监听器添加到你的一个编辑文本中,很可能是editName