Common android controls

Source: Internet
Author: User

1. textview

Effect: although the textview text provided by the system has its own effect, the effect can be achieved only when the focus is obtained,

Therefore, you need to override the isfocused () method of the textview class so that it returns true forever.

Public class scrollforevertextview extends textview {public scrollforevertextview (context) {super (context); // todo auto-generated constructor stub} public scrollforevertextview (context, attributeset attrs) {super (context, attrs);} public scrollforevertextview (context, attributeset attrs, int defstyle) {super (context, attrs, defstyle);} @ overridepublic Boolean isfocused () {return true; // The key is this method }}

Before the code scrollforevertextview added to XML, the package name of the class is located.

 <com.quickeasytrip.view.ScrollForeverTextView                    android:id="@+id/book_hotel_name_verify"                    android:layout_width="wrap_content"                    android:layout_height="wrap_content"                    android:ellipsize="marquee"                    android:singleLine="true"                    android:marqueeRepeatLimit="marquee_forever"                    android:scrollHorizontally="true"                     />

2. editview edit box

When you first open the activity, it is very likely that an input method is automatically displayed because the layout contains an editview control. To remove this annoying Input Method box, you only need to add an attribute to the parent layout of the editview.

Solution: find one in the edittext parent control and set Android: focusable = "true" Android: focusableintouchmode = "true". In this way, the default edittext behavior is truncated!

When entering the username and password in the edit box, the most common problem is that the edit box is blocked by the input method. If you want to edit the next editview, you must disable the input method,

The UI layout is also separated from the input method, and the edittext area is automatically reduced. In this case, user input and further operations are not affected!

Even if the input method is enabled, the user can see the overall UI, Which is comfortable and friendly.

In this case, you only need to add this attribute to the activity of the androidmanifest. xml file.

        android:windowSoftInputMode="adjustResize"

If the login name is of the email type, you can set the Android: inputtype attribute to adapt the pop-up Input Method to the input email format.

Android: inputtype = "textemailaddress": Suitable for inputting emails Android: inputtype = "Number": Suitable for inputting numbers

3. viewpage
To use this control, add this package first. Right-click your project ----> Android tools --------> addsupportlibrary to add the jar package.

Step 1 add controls in XML

 <android.support.v4.view.ViewPager  android:id="@+id/pagerMain"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:layout_below="@+id/advert" />

Step 2: Inherit the pageradapter and add this adapter to viewpager.

Public class mypageradapter extends pageradapter {public list <View> mlistviews = NULL; viewpager mviewpager = NULL; Public mypageradapter (list <View> mlistviews, viewpager) {This. mviewpager = viewpager; this. mlistviews = mlistviews;} @ overridepublic int getcount () {return mlistviews. size () ;}@ overridepublic Boolean isviewfromobject (view arg0, object arg1) {return arg0 == (arg1) ;}@ overridepublic object instantiateitem (view container, int position) {(viewpager) container ). addview (mlistviews. get (position); Return mlistviews. get (position) ;}@ overridepublic void destroyitem (view container, int position, object) {If (mlistviews. size ()> 0) {If (mlistviews. size ()-1)> = position) {mviewpager. removeview (mlistviews. get (position);} // after actual tests, this method often reports subscript out-of-bounds errors, which effectively avoids similar errors }}}

4. Prompt box Dialog

All the prompt class floating boxes inherit from this class dialog. One of the two most commonly used prompt boxes is alertdialog and the other is progresdialog. The former can interact with the user to accept user operations, and the latter prompts the user to have a time-consuming operation in progress. Please wait patiently.

Because it is difficult for your application to match the subject of the system, these two prompt boxes are usually written by yourself. The following is an example of custom progresdialog.

View pdview = factory. inflate (R. layout. pd_view, null); dialog mprogressdialog = new dialog (this, R. style. nobgdialog); mprogressdialog. setcontentview (pdview); // The prompt box mprogressdialog is displayed when the show method is executed. show ();

Code of pd_view

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "150dip" Android: layout_height = "wrap_content" Android: Background = "@ drawable/pd_bg" Android: gravity = "center" Android: Orientation = "horizontal"> <progressbar Android: Id = "@ + ID/loading_process" Android: layout_width = "34dip" Android: layout_height = "34dip" Android: indeterminate = "false" Android: indeterminatedrawable = "@ anim/frame_animation"/> <textview Android: Id = "@ + ID/mpdtext" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: paddingleft = "10dip" Android: text = "Please wait... "Android: textcolor =" @ Android: color/Black "Android: textsize =" 14dip "Android: textstyle =" bold "/> </linearlayout>

Code of the animation list. The rotation effect is smoothest when it reaches 8 frames per frame for 100 milliseconds.

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false" >    <item android:drawable="@drawable/scan_01" android:duration="100"/>    <item android:drawable="@drawable/scan_02" android:duration="100" />    <item android:drawable="@drawable/scan_03" android:duration="100" />    <item android:drawable="@drawable/scan_04" android:duration="100" />    <item android:drawable="@drawable/scan_05" android:duration="100" />    <item android:drawable="@drawable/scan_06" android:duration="100" />    <item android:drawable="@drawable/scan_07" android:duration="100" />    <item android:drawable="@drawable/scan_08" android:duration="100" /></animation-list>

Activity provides a method to manage all the prompts

@ Overrideprotected dialog oncreatedialog (int id, bundle ARGs) {progressdialog dialog = new progressdialog (this); dialog. setmessage ("waiting for mad"); Return dialog ;}

Call showdialog (ID); to display the specific prompt box. During programming, dialog. Show () can also be displayed, as if this method is useless. In fact, when you press back to return the operation

The former will eliminate the prompt box, while the latter will not respond. You need to rewrite the onkeydown () method to eliminate the prompt box.

5. contextmenu context menu

Step 1 override the oncreatecontextmenu () method of activity

@ Overridepublic void oncreatecontextmenu (contextmenu menu, view V, contextmenuinfo menuinfo) {// set context menu title menu. setheadertitle ("File Operations"); // Add context menu item menu. add (0, 1, menu. none, "send"); menu. add (0, 2, menu. none, "marked as important"); menu. add (0, 3, menu. none, "RENAME"); menu. add (0, 4, menu. none, "delete"); super. oncreatecontextmenu (menu, V, menuinfo );}

Step 2 register a view for the context menu. When the view is long pressed, the menu is displayed.

Registerforcontextmenu (View );

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.