(GO). NET developers learn about the second week of Android

Source: Internet
Author: User

This week relatively no spring festival is so idle, daytime also more work to do, every night and my three-year-old daughter to toss to 10 points, really do not have much time to learn. On the basis of the previous week, this week I tried to write a personal management of the Android program, the main realization of the calendar, calendar, handy stickers, such as mass SMS and other functions. The following summarizes their own in the process of some experience and experience, are some of the technical content of the work, I only write down their own ideas, do not paste the specific code. Suffering from the surrounding no one can communicate, most of the functions are their own behind closed doors or reference on-line information to achieve, do not know whether it is reasonable, hope the master can be corrected.

1. Page Layout Tips

Page layout is an important part of Android development, relative to the PC program, because the phone screen less, in order to let users have a good experience, UI design is more important. The design of the page relative to form,android in. NET is also more complex. At first I was directly trying to draw a few controls in layout, position and size is also fixed, the result of a touch, the interface is completely chaotic. Later, the use of some layout components was explored.

    • LinearLayout and Relativelayout.

These two should be the most commonly used two layout components. LinearLayout is generally used to divide the interface into several parts, you can set its orientation, specify whether it is horizontal or vertical. Relativelayout is a relatively well-arranged component, a very useful component. The controls inside it can be arbitrarily set relative to each other, either relative to the adjacent control (layout_toleftof, etc.) or relative to its parent control relativelayout (Layout_alignparentleft, etc.). Relative to the parent control is especially useful, such as sometimes we need to put a control on the bottom, you can use Layout_alignparentbottom.

The layout of the entire page, usually a large linearlayout, divides the page into parts, such as upper and lower. Then, as needed, each part is placed in a relativelayout, where the child control is placed, and the relative position is set.

    • drawable resources, I feel is a very useful thing, very similar to web development in CSS. We can specify a drawable for a control, which is typically placed in res/drawable. In particular, one can set the dynamic state of a control, such as a button-disabled color, a normal color, and a pressed color.
    • Dynamically sets the height and width of the control.

Android does not seem to be directly in the program to set the control width and height of the properties, need to use the layoutparams, such as

Linearlayout.layoutparams param = (linearlayout.layoutparams) textview.getlayoutparams ();

Param.width = colwidth;

Param.height = Colheight;

Textview.setlayoutparams (param);

    • Dynamically sets the top and bottom left and right icons of the control.

This is a cool feature of the. NET controls, and we can add different icons to the top and bottom four directions of the control and change them dynamically in the program.

drawable dw_left = Getresources (). getdrawable (R.drawable.schedule_type);

Dw_left.setbounds (0,0,dw_left.getminimumwidth (), Dw_left.getminimumheight ());

Textview.setcompounddrawables (null, NULL, dw_left, NULL);

2. Custom Control Wedgit

Beginners do not find it difficult to write a control on their own. In fact, in Android is still very good implementation. Sometimes I need to add some controls to the icon, or the border, and so on, its own properties do not implement similar functions. We simply encapsulate the original control in a simple way. For example, I want to use the border of the edittext,edittext itself is not a border, we can simply write an extended EditText class, where the main use of the OnDraw (canvas canvas) method. We overload this method in subclasses, and we can draw some borders for the controls.

protected void onDraw (canvas canvas) {

TODO auto-generated Method stub

Super. OnDraw (canvas);

Paint paint = new paint ();

Paint.setstrokewidth (1);

Paint.setstyle (Style. STROKE);

Paint.setcolor (Android.graphics.Color. Blue);

Paint.setantialias (true);

RECTF RECTF = new RECTF (2,0,this. getwidth ()-2,this. getheight ()-2);

Canvas.drawroundrect (RECTF, 8, 8, paint);

}

3. Techniques for using the GridView and ListView

The GridView and ListView are the most used rendering controls, and we already know that it typically overloads a adapter class to also implement data binding. With this adapter, we can achieve a lot of awesome features.

    • Sets the background of the selected item or changes the state of the control in the selected item

I think of two ways to achieve this effect, specifically which is better, because there is no actual development experience, I am not very clear, looking master pointing.

A) implemented in the Onitemclick event in the grid. Onitemclick (Adapterview arg0, View arg1, int arg2,long arg3) have four parameters, and we often use the second and third parameters. Arg1, refers to the view of this subkey, through which we can get the corresponding child controls. Arg2, refers to the position of the current item in the list.

We want to change the state of the currently selected item and, of course, restore the state of the last selected item, so we need a middle temporary variable at the page level to save the previous selection. For example, the following code is to add an icon to the bottom of a textview in the currently selected item.

public void Onitemclick (Adapterview arg0, view view, int Position,long arg3) {

drawable dw_left = Getresources (). getdrawable (R.drawable.date_convert);

Dw_left.setbounds (0,0,dw_left.getminimumwidth (), Dw_left.getminimumheight ());

TextView TextView = (TextView) View.findviewbyid (R.id.tvtext);

Textview.setcompounddrawableswithintrinsicbounds (NULL, NULL, NULL, DW_LEFT);

if (lastselectedcol! = null) {

Lastselectedcol.setcompounddrawableswithintrinsicbounds (NULL, NULL, NULL, NULL);

}

Lastselectedcol = TextView;

}

b) implemented in adapter. Onitemclick event We're still going to use it, this time we're going to take advantage of its third parameter, Arg2, where the current item is in the list.

First we want to set a property for adapter to pass the position of the current item to adapter.

public void Setselectedcol (int position) {

Selectedcol = position;

}

Once we have the position of the selected item, we can deal with it in the GetView method of the adapter. The processing method is the same as above, the control is first obtained, and then the corresponding processing.

if (Selectedcol = = position) {

Handling controls

}

Finally, in the Onitemclick event of the grid, we don't forget to use the Myadapter.notifydatasetchanged method to refresh the control.

    • Dynamically change the icon for each item in the GridView or ListView

For example, to make a similar text message in the iphone function, click the Edit button, the list of all the text messages to the left of the display an action icon, click on the action icon, the right side will add a delete button, then click Delete button, delete the current item. There is no detailed code here, just a summary of the implementation points:

A. Adpater to add a property that indicates when the ListView is in an editable or read-only state.

public void Setedit (Boolean edit) {

This.isedit = edit;

}

B. In the GetView method in Adpater, depending on the Isedit property, the left icon is displayed and the corresponding event is added.

C. The edit button of the parent page in the Click event, dynamically sets the value of the Isedit in Adpater.

4. Scroll load page for listview

Although Android does not have pagination controls like. NET, the ability to display data in pagination is well implemented. As long as we determine in the Onscroll event whether the ListView is scrolling at the bottom of the if (firstvisibleitem + visibleitemcount = = Totalitemcount), and then dynamically changing the data source when the Increase the entry for the data source.

If we take the data of SQLite, we can load it directly because of the fast speed. Don't forget that the query method of Sqlitedatabase can be paged.

If you are taking a network, you may want to use async, and at the same time we can use a control to display "loading". When you are finished loading, hide the control again.

5. Jump for child activity in tab control

The tab control at the top or bottom of the program is a more versatile feature, many programs have multiple tabs at the bottom, and a different tab jumps to different function modules. Android also has a control called Tabhost to implement this feature. The concrete implementation is very simple, must use the tabactivity. Your activity needs extents tabactivity.

In the use of the process, I found a problem, such as I a Main page, which used a tab, the second item opens a page, and then I want to jump from a page to page B, b after the completion of the operation and then return to a page, when the problem, a page because it is a sub-page of the main page, I need the lower part to always show tab, but after the B page is intent to a page, the tab below is missing. If I return to the main page from page B, it will not open the a page by default. This question began to confuse me for a long time, will come to find a method on the Internet, the main page of the Tabhost control set to a global static variable, so that the B page run back to the main page after the code, Plus MainActivity.tabHost.setCurrentTab (1), so the main page opens, but also point to a page.

The above problem solved, again a new question, originally a page is a sub-page, the following is displayed the page tab, if I jump from page A to page b, want B page also keep the following tab, how should this be done? On the internet for a long time, only to find a use Groupview implementation of the. It is using a Activitygroup page. This is called the BaseGroup page. In the Tab tab of the main page, click on the Open BaseGroup page and jump to the a page via BaseGroup. Refer to the following article for details.

http://hkp.iteye.com/blog/1185482

6. Get Network data asynchronously

There are usually two ways to get network data asynchronously, using the handler+ thread or the Asynctask method, and I've just done a simple experiment, feeling that the handler+ thread is clearer and easier for beginners to master. The specific implementation can refer to the following article, written in very detailed.

Http://www.cnblogs.com/JerryWang1991/archive/2012/03/09/2388312.html

7. Summary of two weeks

Through two weeks of study, the overall feeling of Android development is relatively good to get started, it should use the breadth of knowledge should not be compared to. NET project development. With a two-week time, it is quite easy to master basic and common skills and do some practical development.

For learning resources, my advice is to look directly at the official Android website, especially some API instructions. Before you use a control or feature, you should first look at its API description, and probably understand what its methods and properties are, so it's clear what it can do to meet your needs.

Looking at someone else's code or sample code is a great way to learn. Android is an open platform that can also find a lot of code on the Web. But to really turn it into something of its own, without it, or try to do it yourself, just do it!

Have time to hope to communicate with you more, good luck ...

Related Article

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.