/*************************************** **************************************** *************
* Author: conowen @ Dazhong
* E-mail: conowen@hotmail.com
* Http://blog.csdn.net/conowen
* Note: This article is original and only used for learning and communication. For more information, indicate the author and its source.
**************************************** **************************************** ************/
Sometimes to make the code clearer, you need to create a new class file (not inheriting activity) in a new Java document. However, this new class file must be used in main. controls defined in the XML file. In this way, you can re-write the constructor of the control in the newly created class file.
Step: (for example)
1. Define the newly created control or variable in the class first.
AutoCompleteTextView autoinput = null;Context ct=null;
2. Right-click the new class file ------> source --------> Generate constructor using fields. Select the variable or control to be constructed
public MyTextWatcher(Context ct, AutoCompleteTextView autoinput) {super();this.ct = ct;this.autoinput = autoinput;}
3. You can use these controls. Because the newly created class file does not inherit from the activity and does not set the layout file, you cannot use findviewbyid to find the control. Therefore, you need to use this method.
4. In addition,
Context ct=null;
Is the context type.
For example, when constructing an arrayadapter, the first parameter is context. If it is in activity, it can be this, but the newly created class does not inherit the activity. Therefore, the constructor is required, build a context CT.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ct,android.R.layout.simple_dropdown_item_1line, str);
5. When calling this class, you can call the following in the original activity. Parameters are the two parameters that need to be constructed in the new class. It is of the context and autocompletetextview types. Mytextwatcher is the class name of the new class.
autoinput.addTextChangedListener(new MyTextWatcher(MyDictionaryActivity.this,autoinput));