Android from ignorance to knowing--no.2

Source: Internet
Author: User

these days, although encounter bottlenecks, but also getting better, because no contact with Android, so as a novice will not give themselves too high requirements. Compared to last year's distributed resource retrieval, this year's mobile development is really simple, although its nature is not much different, but from the user experience to inspire the interest of every learner, this may be the charm of Android ...

The day before yesterday to do "Phone Dialer", the last load to the simulator when do not know where the wrong, the system is always automatically shut down, angry also did not respond to it; yesterday did a "message transmitter", but also met with the former similar problems, but finally after debugging is a successful development, so adjusted the former, The same solution, outside Rui better ~ ~ ~ Then take the "SMS transmitter" as an example to talk about some of their learning experience it ...

First one:

first of all, we say that the basic structure of the message sender, we can see from the general can be divided into five parts, in fact, is two tips + two input boxes, there is a Send 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 defined text message input box, the top "@+id/et_word" is to add a R.java file named Et_word ID, that is, the declaration of our input box, lines= "5" Specifies that the input box is five lines, this length can meet our needs And the following four layout properties define the length and relative position of the text box, and the value of the property can be well understood, it is interesting, the third means that the text box is located under the ID tv_ism, and tv_ism is what we see "Please enter the text message content" Finally, the type of text message input box is defined. The other pieces are similar, and of course there are a lot of properties, and you can look at their APIs to find out.

UI design is relatively simple, but also hope that you have a lot to forgive, the next is to give its corresponding functions, and then on 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;}} }

We start from the actual, want to make SMS sender can send text messages, first to find the Send button, but when we click on the button, the system will find the text message content and want to send the number, and then realize its function, is generally divided into these three steps.

So we first find the button: Button bt_sent= (button) Findviewbyid (r.id.bt_sent); Bt_sent.setonclicklistener (this); Here the side involves a few commonly used methods, we see its surface meaning can also understand, first through the ID to find the button, and then call its method to set the Click event; below onclick (View v) For the implementation of the interface Onclicklistener, when the obtained ID is r.id.bt_sent, a series of operations are performed; the top Et_number, Et_word, and Bt_sent are the same, first finding them by ID, Then use their own method to extract phone numbers and text messages, the next is to determine whether the phone number or text message content is empty, I believe we all have this common sense, both of them are empty can not send text messages; About toast everyone may go to find out, translated into Chinese as "toast bread", It is mainly to give the user a hint, it calls the method has three parameters, this is not a good beginning to understand, but through this example, you can also guess its general meaning, where the third parameter is the length of the display, and finally do not forget to call the Toast Show () return value. If the number or content is not empty, then execute else in the statement, extract the text message content, if the sending content is too long, more than the specified 70 characters or 160 characters will be segmented, this time to use the arraylist<string> words= Smsmanager.dividemessage (Word), then iterates over it, smsmanager.sendtextmessage (number, NULL, str, NULL, NULL); There are five parameters, The first is the contact phone number to be sent, the second is where the SMS came from, and now our country's operators do not support this feature; the third is to send the text message content; The fourth parameter is responsible for reporting whether the text message was sent successfully, and the fifth is to send the delivery report, which is now set to null.

         One more thing to note is Smsmanager Smsmanager=smsmanager.getdefault (); this statement , the package that was previously imported by default has expired, so you need to import a new package, which will be prompted.
         ok, so a simple SMS transmitter is set up, start two simulator can be experimented, as shown in the The difference between sending short text and long text is also straightforward. Today will continue to learn more about the four layouts, the UI era comes, please look forward to ...
 

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.