The first step involved in technology Pager controls, Menu use, and text message call: we have created the main interface for creating the previous Menu article, but it is not enough to show only such information to attract the interviewer, so we need to add more requirements. If we successfully let the HR or interviewer install this program, we have to let him not only view our information, but also have other more convenient functions, at this time, I thought of the function of directly sending text messages and making phone calls. I decided to add these two functions to the menu on our main interface. First, rewrite onCreateOptionsMenu and onOptionsItemSelected. In onCreateOptionsMenu, we create a menu item. In onOptionsItemSelected, we process the events clicked for each menu item. This is the code for creating a menu item:
@ Override public boolean onCreateOptionsMenu (Menu menu) {menu. add (0, 1, 1, "contact me by phone"); menu. add (0, 2, 2, "Notify me via SMS"); return super. onCreateOptionsMenu (menu );}
This is the code for processing click events:
@ Override public boolean onOptionsItemSelected (MenuItem item) {if (item. getItemId () = 1) {// call} else if (item. getItemId () = 2) {// SMS notification} return true ;}
Next we can focus on the business. We should be familiar with Intent. Intent can be used to easily jump to Activity and start Service. We use Intent's public Intent (Context packageContext, Class Cls). Another Intent constructor is also very useful: public Intent (String action, Uri uri ). It allows us to input an action address and a data URI to perform an operation. Intent has many action addresses defined by the system, this includes the text message and call address we are going to use. For details, see here. Let's take a look at our call example:
// Call Intent intent = new Intent (Intent. ACTION_CALL, Uri. parse ("tel:" + MY_PHONE_NUMBER); startActivity (intent );
Simple code, Intent. ACTION_CALL indicates the call program. At the same time, we pass in a Uri that represents the data. For the Uri format, the Input in this document indicates that it is tel: connect the phone number, this format. Let's look at the text message example:
// SMS notification Uri uri = Uri. parse ("smsto:" + MY_PHONE_NUMBER); Intent intent = new Intent (Intent. ACTION_SENDTO, uri); intent. putExtra ("sms_body", "Hello, thank you. "); StartActivity (intent );
We use Intent. ACTION_SENDTO to pass an smsto: connect the phone number to send a text message to someone. Similarly, emailto: You can send an email after receiving the email address. At the same time, we also need to add content to the text message, as shown in the Code. Here you can also write more confidently: "Hello, thank you, congratulations, you have passed our interview."
The menu we created is like the following, and there is a text message interface:
Step 2: the Avatar is ready before you click on our circular avatar. The clicking event has been added in the previous article, but the PhotoClickListener has not been defined. This class is very simple, directly jump to the class we want to jump to. The next class is named PagerActivity. The Code is as follows:
private class PhotoClickListener implements View.OnClickListener { @Override public void onClick(View view) { Intent i = new Intent(context, PagerActivity.class); startActivity(i); } }
At this point, MainActivity has been completed. For the complete code, see here.
Step 3: Design PagerActivity. You have set the name of the class to jump to after clicking the Avatar in the previous step, and then create it. Let's first look at the effect:
This is a new control that was released on android 3.0. However, google released a compatible package for earlier android versions, allow lower-version programs to use this control, the name of this compatible package is android-support-v4.jar. With this compatibility package, we can easily achieve this effect of Sliding between the left and right to switch the View. We create an Activity and start the layout. The layout is as follows:
Because we use a custom control, we need to write the package name of the control. Here I also define a style to unify the background of the Application for easy maintenance and modification. The style is defined as follows:
At this time, we still cannot see the effect, because we have not added data to ViewPager. ViewPager is also based on Adapter to process data. It has a unique Adapter called PagerAdapter, we create an internal class that inherits PagerAdapter and rewrites the method of this rewrite. If you have written the ListView, it is easy to get started with these methods. It doesn't matter if you have never used ListView, I can see the meaning of these methods once.
First, we need to have two datasets to store the View for switching and the corresponding display title:
The two variables added to viewList are the two views we will design later. Now you can see how the Adapter was created:
private class MyPagerAdapter extends PagerAdapter { @Override public int getCount() { return viewList.size(); } @Override public Object instantiateItem(ViewGroup container, int position) { //return super.instantiateItem(container, position); container.addView(viewList.get(position)); return viewList.get(position); } @Override public void destroyItem(ViewGroup container, int position, Object object) { //super.destroyItem(container, position, object); container.removeView(viewList.get(position)); } @Override public CharSequence getPageTitle(int position) { //return super.getPageTitle(position); return titleList.get(position); } @Override public boolean isViewFromObject(View view, Object o) { return view == o; } }
I can't say anything about this Code. The method name is easy to understand, and it is basically in this mode. Then I will write a PagerAdapter, which is probably the same code, so I will not explain it. At the end of the next article, we will design the two views displayed in PagerView. I hope this program can stimulate everyone's imagination, come up with more such ideas, do some practical small programs, and share them. I also accept suggestions and comments from everyone. Finally, the program has been hosted on Github. You are welcome to download it. If you are too lazy to write your resume for Android, you can check it out directly, you can modify the data and photos.