Android learning notes (15) ---- precautions for creating a class in the android Project

Source: Internet
Author: User

/*************************************** **************************************** *************
* 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));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.