【问题标题】:Android TextWather object. How java anonymous class is working ?Android TextWather 对象。 java匿名类是如何工作的?
【发布时间】:2014-05-08 04:41:10
【问题描述】:
private TextWatcher billEditTextWatcher = new TextWatcher() 
   {
      // called when the user enters a number
      @Override
      public void onTextChanged(CharSequence s, int start, 
         int before, int count) 
      {         
         // convert billEditText's text to a double
         try
         {
            currentBillTotal = Double.parseDouble(s.toString());
         } // end try
         catch (NumberFormatException e)
         {
            currentBillTotal = 0.0; // default if an exception occurs
         } // end catch 

         // update the standard and custom tip EditTexts
         updateStandard(); // update the 10, 15 and 20% EditTexts
         updateCustom(); // update the custom tip EditTexts
      } // end method onTextChanged

      @Override
      public void afterTextChanged(Editable s) 
      {
      } // end method afterTextChanged

      @Override
      public void beforeTextChanged(CharSequence s, int start, int count,
         int after) 
      {
      } // end method beforeTextChanged
   }; // end billEditTextWatcher

这是来自专业人士编写的小费计算器应用程序的一段代码。有人可以解释这是如何工作的吗?

通常我只是编写以下内容来创建一个新对象。

TextWatcher billEditTextWatcher = new TextWatcher();

我了解私人做什么。但是新对象的创建过程中怎么会有方法呢?它基本上是在做它所说的吗?覆盖 TextWatcher 类中的原始方法?

我希望这个问题有意义,因为我很困惑。

提前致谢!

【问题讨论】:

    标签: java android object textwatcher


    【解决方案1】:

    这是 Java 中匿名类的示例。你没有任何TextWatcher java 文件,你在初始化的时候声明了类的内容。

    http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

    【讨论】:

    • 请检查 java 匿名类是如何工作的,它作为一个表达式。希望有帮助
    • 感谢蓝和海夫!
    • 不好意思回复错人了,我用手机接听
    【解决方案2】:

    顾名思义,TextWatcher 将感知EditText 的所有事件

    就像你在可编辑区域写东西一样。

    它有以下回调(观看状态。)

    1. 按键 - beforeTextChanged();
    2. 按键 - afterTextChanged();
    3. 文本更改 - onTextChanged();

    所有回调方法都包含由事件生成器传递的相关数据。比如哪个键被按下,Unicode(ASCII) 等。

    基本上,TextWatcher 用于在向其中输入数据时监视 EditText 或 MultiLine EditText。我们可以执行操作,观察EditText中输入了哪些字符或输入了多少个字符。

    技术说明:

    1. 如果你想使用TextWatcher,那么你需要用TextWather对象来抵抗你的EditText

    例如

    EditText editTextPassword;  // Some EditText object.
    
    TextWatcher billEditTextWatcher = new TextWatcher();  // TextWather object creation
    
    editTextPassword.addTextChangedListener(billEditTextWatcher );  // EditText registation with Textwather object.
    
    1. 默认情况下,TextWather 的所有回调都是空的,这就是为什么您需要根据您的要求给出所有回调的定义。

      private TextWatcher billEditTextWatcher = new TextWatcher() 
       {
      
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) 
        {         
      
            // you code is here while onTextChanged.
      
        } 
      
        @Override
        public void afterTextChanged(Editable s) 
        {
      
            you code is here while afterTextChanged.
      
        } 
      
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
           int after) 
        {
      
           // you code is here while beforeTextChanged.
      
        } 
      }; 
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多