Android from ignorance to knowing--no.2

Source: Internet
Author: User
Tags gettext

these days, though, encounter bottlenecks. But it's getting better. Because there was no previous contact with Android, so as a novice will not give themselves too high requirements. This year's mobile development is a bit simpler than the distributed resource search that was done last year. Although the nature is not very different, but from the user experience to inspire each learner's interest, this may be the charm of Android ...

The day before yesterday to do "telephone dialer", finally loaded into the simulator when do not know where the wrong, the system always self-closed, angry also did not talk about it. Yesterday did "SMS Transmitter", also met with the former similar problems, but finally after debugging is a successful development bar. So adjust the former, the same way of solution. Outside Rui Well ~ ~ ~ Then take "SMS Transmitter" as an example to talk about some of their learning experience it ...

First one:

Let's talk about the basic structure of this message transmitter, as you can see, it can be broadly divided into five parts. In fact, it is two hints + two input boxes, and the other sends a button. So how do these five parts come out, first look at a small piece of code:

<edittext        android:id= "@+id/et_word"        android:lines= "5"        android:layout_width= "Fill_parent        " android:layout_height= "Wrap_content"        android:layout_below= "@id/tv_ism"        android:layout_centervertical= " True "        android:inputtype=" Textmultiline "></EditText>

This code is to define the text message input box, the top "@+id/et_word" is to add a R.java file named Et_word ID, which is the declaration of our input box. Lines= "5" stipulates that this input box is five elements, this length is generally able to meet our needs, while the bottom of the four layout properties define the length and relative position of the text box, and its property values can be well understood. is very interesting, the third means that the text box is located under the ID tv_ism, and tv_ism is what we have shown "Please enter the text message content", and finally defined the type of text message input box. A few other pieces are similar, and of course there are a lot of properties that you can look at their APIs to see.

UI design is relatively simple, but also hope that you have a lot to forgive. The next step is to give it the corresponding function, and then a chunk of code:

public class Mainactivity extends Activity implements Onclicklistener {private EditText et_word;private EditText et_nu    Mber;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);         Setcontentview (R.layout.activity_main);         Et_number= (EditText) Findviewbyid (R.id.et_number);        Et_word= (EditText) Findviewbyid (R.id.et_word);        Button bt_sent= (button) Findviewbyid (r.id.bt_sent);    Bt_sent.setonclicklistener (this); } @Overridepublic void OnClick (View v) {switch (V.getid ()) {case r.id.bt_sent:string word=et_word.gettext (). toString (). Trim (); String Number=et_number.gettext (). toString (). Trim (); if (Textutils.isempty (word) | | Textutils.isempty (number)) {Toast.maketext (this, "phone or SMS content cannot be empty", Toast.length_short). Show (); return;} else{Smsmanager Smsmanager=smsmanager.getdefault (); Arraylist<string> words=smsmanager.dividemessage (word); for (String str:words) {smsmanager.sendtextmessage (number, NULL, STR,NULL, NULL);   }}break;}} }

          Let's start with the actual situation. To enable SMS senders to send text messages. First we need to find the Send button, but when we click on the button, the system will find the text message and the number you want to send, and then realize its function, which is basically divided into three steps.

         We first find Button:button bt_sent= (Button) Findviewbyid (r.id.bt_ Sent); Bt_sent.setonclicklistener (this); Here the side involves a few frequently used methods, we see the surface meaning can also understand, first find the button by ID, and then call its method set click event. The onclick (View  v) below is the implementation of the interface Onclicklistener. A sequence of operations is run when the obtained ID is r.id.bt_sent, and the upper Et_number, Et_word, and Bt_sent are the same, first to find them by ID. Then use their own method to extract phone numbers and text messages, the next step is to infer whether the phone number or text message content is empty, I believe everyone has this common sense, both of them are empty can not send text messages. About toast everyone can go to check, translated into Chinese as "toast bread." It is primarily a hint to the user. It calls the method has three parameters, this is not very good understanding, but through this example you can guess the general meaning of it, the third parameter is the display length, and finally do not forget to call the Toast Show () return value. If the number or content is not empty, then run the statement in else, extract the text message content, assuming that the content is too long, more than the specified 70 characters or 160 characters will be segmented. The arraylist<string> words=smsmanager.dividemessage (word) is used and then traversed, smsmanager.sendtextmessage (number, NULL, str, NULL, NULL); There are five parameters, the first is the contact phone number to be sent, and the second is where the text message came from, and now our country's executor does not support this feature; the third is to send the text message content; The fourth parameter is responsible for reporting whether the SMS was sent successfully. The fifth is whether to send a delivery report. This is now a no-go, and can be set to null.

It is also important to note that Smsmanager Smsmanager=smsmanager.getdefault (); This statement, which was previously imported by default, has expired, so you need to import a new package. This will be a hint.
OK, so a simple SMS transmitter is built. Launch two simulators to experiment, as you can see, the difference between sending short text and long text is straightforward. Today you will continue to learn more about the four layouts. The advent of the UI era, please look forward to ...

Android from ignorance to knowing--no.2

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.