TextWatcher&放大器;编辑不能解析为一个类型 - 从"学习Android和QUOT; [英] TextWatcher&Editable cannot be resolved to a type--from "Learning Android"

查看:393
本文介绍了TextWatcher&放大器;编辑不能解析为一个类型 - 从"学习Android和QUOT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读这本书学习Android的,我输入71页上的code,月食显示错误:


  

编辑不能被解析为一个类型...


  
  

TextWatcher不能被解析为一个类型...


  
  

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


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

我的android的新手,任何帮助将AP preciated。

我的code是:

 包com.marakana.yamba;进口winterwell.jtwitter.Twitter;
进口winterwell.jtwitter.TwitterException;
进口android.app.Activity;
进口android.graphics.Color;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;
进口android.widget.Toast;
公共类StatusActivity扩展活动实现OnClickListener,TextWatcher {
    私有静态最后弦乐TAG =StatusActivity;
    EDITTEXT的EditText;
    按钮updateButton;
    叽叽喳喳叽叽喳喳;
    TextView的textCount;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.status);        //查找意见
        EDITTEXT =(EditText上)findViewById(R.id.editText1);
        updateButton =(按钮)findViewById(R.id.buttonUpdateStatus);
        updateButton.setOnClickListener(本);        textCount =(的TextView)findViewById(R.id.textCount);
        textCount.setText(Integer.toString(140));
        textCount.setTextColor(Color.GREEN);
        editText.addTextChangedListener(本);        叽叽喳喳=新的Twitter(hookits,TW ** 123);
        twitter.setAPIRootUrl(http://yamba.marakana.com/api);
    }    //异步帖子到Twitter
    类PostToTwitter扩展的AsyncTask<字符串,整数,字符串> {
        //调用启动后台活动
        @覆盖
        保护字符串doInBackground(字符串...状态){
            尝试{
                winterwell.jtwitter.Status状态= twitter.updateStatus(状态[0]);
                返回status.text;
            }赶上(TwitterException E){
                Log.e(TAG,e.toString());
                e.printStackTrace();
                返回无法发帖
            }
        }
        //当有要更新状态调用
        @覆盖
        保护无效onProgressUpdate(整数...值){
            super.onProgressUpdate(值);
        }        //调用一次后台活动已完成
        @覆盖
        保护无效onPostExecute(字符串结果){
            Toast.makeText(StatusActivity.this,结果,Toast.LENGTH_LONG).show();
        }
    }    // TextWatcher方法
    公共无效afterTextChanged(可编辑状态文本){
        诠释计数= 140 - statusText.length();
        textCount.setText(Integer.toString(计数));
        textCount.setTextColor(Color.GREEN);
        如果(计数小于10)
            textCount.setTextColor(Color.YELLOW);
        如果(计数℃,)
            textCount.setTextColor(Color.RED);
    }    公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT){}
    公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){}
    //按钮被点击时调用//
    公共无效的onClick(视图v){
        字符串状态= editText.getText()的toString()。
        新PostToTwitter()执行(状态);
        。//twitter.setStatus(editText.getText()的toString());
        Log.d(TAGonClicked);
    }
}


解决方案

您缺少此导入:

 进口android.text.TextWatcher;

添加它,它应该工作。

注意:
添加完导入Eclipse可能会告诉你,你有未实现的方法。只需添加它们,移动你的code,以适应它。

修改

另外补充:

 进口android.text.Editable;

I am reading the book "Learning Android", I typed the code on page 71, the eclipse show the error:

Editable cannot be resolved to a type...

TextWatcher cannot be resolved to a type...

The method addTextChangedListener(TextWatcher) in the type TextView is not applicable for the arguments ...

I have checked the code between my eclipse and the book for several times, but I can't find the difference between them.

I am a newbie of android, any help will be appreciated.

My code is:

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

解决方案

You are missing this import:

import android.text.TextWatcher;

Add it, and it should work.

Note: After you add the import Eclipse might tell you that you have "unimplemented methods". Just add them and move your code around to fit it.

Edit

Also add:

import android.text.Editable;

这篇关于TextWatcher&放大器;编辑不能解析为一个类型 - 从&QUOT;学习Android和QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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