TextWatcher&Editable无法解析为某种类型 - 来自"学习Android"

pvd*_*pvd 0 android android-widget android-manifest android-layout

我正在阅读"学习Android"一书,我在第71页输入了代码,eclipse显示错误:

可编辑无法解析为类型...

TextWatcher无法解析为类型...

TextView类型中的方法addTextChangedListener(TextWatcher)不适用于参数...

我已经多次检查了我的日食和书之间的代码,但我找不到它们之间的区别.

我是android的新手,任何帮助将不胜感激.

我的代码是:

package com.marakana.yamba;

import winterwell.jtwitter.Twitter;
import winterwell.jtwitter.TwitterException;
import android.app.Activity;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class StatusActivity extends Activity implements OnClickListener, TextWatcher{
    private static final String TAG = "StatusActivity";
    EditText editText;
    Button updateButton;
    Twitter twitter;
    TextView textCount;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.status);

        //Find views
        editText = (EditText) findViewById(R.id.editText1);
        updateButton = (Button) findViewById(R.id.buttonUpdateStatus);
        updateButton.setOnClickListener(this);

        textCount = (TextView) findViewById(R.id.textCount);
        textCount.setText(Integer.toString(140));
        textCount.setTextColor(Color.GREEN);
        editText.addTextChangedListener(this);

        twitter = new Twitter("hookits", "tw**123");
        twitter.setAPIRootUrl("http://yamba.marakana.com/api");
    }

    // Asynchronously posts to twitter
    class PostToTwitter extends AsyncTask<String, Integer, String> {
        // Called to initiate the background activity
        @Override
        protected String doInBackground(String... statuses){
            try{
                winterwell.jtwitter.Status status = twitter.updateStatus(statuses[0]);
                return status.text;
            } catch (TwitterException e){
                Log.e(TAG, e.toString());
                e.printStackTrace();
                return "Failed to post";
            }
        }
        // Called when there's a status to be updated
        @Override
        protected void onProgressUpdate(Integer... values){
            super.onProgressUpdate(values);
        }

        // Called once the background activity has completed
        @Override
        protected void onPostExecute(String result){
            Toast.makeText(StatusActivity.this, result, Toast.LENGTH_LONG).show();
        }
    }

    //TextWatcher methods
    public void afterTextChanged(Editable statusText){
        int count = 140 - statusText.length();
        textCount.setText(Integer.toString(count));
        textCount.setTextColor(Color.GREEN);
        if (count < 10)
            textCount.setTextColor(Color.YELLOW);
        if (count < 0)
            textCount.setTextColor(Color.RED);
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){}
    // Called when button is clicked//
    public void onClick(View v){
        String status = editText.getText().toString();
        new PostToTwitter().execute(status);
        //twitter.setStatus(editText.getText().toString());
        Log.d(TAG, "onClicked");
    }
}
Run Code Online (Sandbox Code Playgroud)

kas*_*rch 6

您缺少此导入:

import android.text.TextWatcher;
Run Code Online (Sandbox Code Playgroud)

添加它,它应该工作.

注意: 添加导入后,Eclipse可能会告诉您"未实现的方法".只需添加它们并移动代码即可适应它.

编辑

还添加:

import android.text.Editable;
Run Code Online (Sandbox Code Playgroud)