[Android basic learning 3] -- basic controls toast, edittext, radiogroup, and radiobutton

Source: Internet
Author: User

 Disclaimer: The book "Secrets of Android Application Development", which records the logs of the book, references the relevant code and summary, and has no commercial use, it is completely a record of self-learning, and many problems will inevitably occur in learning just now. If there are any mistakes, please criticize them a lot.

Continue learning the basic controls;

1. The toast control is displayed.

This control is mainly used to display information in Android. For example, when you exit the program, the system prompts the player to "Update" and other information. When you enter text in the input box, the system prompts the player to "enter up to 10 characters.

 

Instance analysis: Use the toast control to display the content of the sent text message when receiving the short message.

[Focus]: 1. Use the toast control; 2. How to send and monitor the sending and receiving of SMS messages.

 

[Problem ]:

1. When the simulator runs the program, the message "emulator-5554 disconnected!" is reported! Cancelling ...... Error. That is, the simulator cannot be connected.

 

In this case, I found it in Google. There are three solutions:

1. emulator-AVD-wipe-Data
2. Reset Abd
3. Restarting the eclipse again

 

Solution 1: [the current method successfully solves my problem]

In eclipse, just change the settings when you run.
Right click your project and there is a run configrations in the run,
There is an option in which Wipe records will be destroyed every time you run it. You can just select it.


Emulator-wipe-Data
Or select "Wipe Out user data"

The solution Reference Network: http://ophone8.com/thread-4046-1-1.html

Similarly;
Http://androidforums.com/application-development/5398-android-help-emulator-5554-disconnected.html

First, close (or force close) your current AVD.
Open Android SDK and AVD manager, choose your virtual device, click "start..." then check the "Wipe user data" Mode then click "launch ".

 

The cause of the problem occurs after AVD is manually disabled. If AVD is not started, you can turn it off. Of course, the connection fails (Disconnected !), Cancel ....

 

Important instance source code:

1. If you want to send and receive text messages, you must create a class that inherits broadcastreceiver and rewrite its onreceive method. This method is triggered when you receive the text message.

Public class smsreceiver extends broadcastreceiver {// This method is triggered when you receive the SMS. Public void onreceive (context, intent) {/* Bundle: API: A mapping from string values to various parcelable types. * The bundle class is used to carry data. Similar to map, it is used to store values in the form of key-Value Name-value pairs. * Compared with map, it provides various common types of putxxx ()/getxxx () methods, such as putstring ()/getstring () and putint ()/getint (), * putxxx () is used to put data into the bundle object. The getxxx () method is used to obtain data from the bundle object. * The bundle actually uses the hashmap <string, Object> type variable to store the value put by the putxxx () method: * When the getxxx () method of the bundle object is called, the method retrieves data from the variable and converts the data type to the xxx method. The getxxx () method returns the converted value. */Bundle = intent. getextras (); object message [] = (object []) bundle. get ("PDUS"); smsmessage [] = new smsmessage [message. length]; for (INT n = 0; n <message. length; n ++) {smsmessage [N] = smsmessage. createfrompdu (byte []) Message [N]);/* Create an smsmessage from a raw PDU. source code of the createfrompdu method in the smsmessage class: public static smsmessage createfrompdu (byte [] PDU) {smsmessagebase wrappedmessage; int activephone = telephonymanager. getdefault (). getphonetype (); If (phone_type_cdma = activephone) {wrappedmessage = com. android. internal. telephony. CDMA. smsmessage. createfrompdu (PDU);} else {wrappedmessage = com. android. internal. telephony. GSM. smsmessage. createfrompdu (PDU);} return New smsmessage (wrappedmessage);} * // generate a toasttoast toast = toast. maketext (context, "text message content:" + smsmessage [0]. getmessagebody (), toast. length_long);/* Public String getmessagebody () returns the message body as a string, if it exists and is text based. * /// set the toast display position // toast. setgravity (gravity. top | gravity. left, 0,200); // display the toasttoast. show ();}

The above is a trigger class specifically used to process SMS reception, but it must be configured as a listener service.

<uses-permission android:name="android.permission.RECEIVE_SMS"/>    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >        <activity android:label="@string/app_name" android:name=".Examples_04_05Activity" >            <intent-filter >                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <receiver android:enabled="true" android:name="SMSReceiver">            <intent-filter>                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>            </intent-filter>        </receiver>    </application>

When the program is running, it needs to simulate sending text messages and switch to the ddms mode, for example:

The running effect is shown in the following figure: the program startup effect is shown in the left figure, and the effect after the text message is sent in the right figure.

1. Edit the edittext Control

For the edittext control, you can set the event listening setonkeylistener for it, and implement the onkey method. When you press the button, this event is triggered, and the content in the control can be obtained through gettext.

If the user does not enter data, use edittext. sethint ("enter an account !"); To implement information prompts.

 

II,Single choice (radiogroup, radiobutton)

To implement the effect of a Single-choice button group, you must use the combination of radiogroup and radiobutton, for example:

<RadioGroup    android:id="@+id/RadioGroup01"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="vertical"    android:layout_x="3px"    android:layout_y="54px" >    <RadioButton      android:id="@+id/RadioButton1"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="@string/RadioButton1"    />    <RadioButton      android:id="@+id/RadioButton2"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="@string/RadioButton2"    /></RadioGroup>

 

The selection button on the interface is controlled by radiobutton, and the answer to the single-choice button group is controlled by radiogroup. when defining the radiogroup, when listening to The setoncheckedchangelistener event of the radiogroup:

/* Set event listening */m_radiogroup.setoncheckedchangelistener (New radiogroup. oncheckedchangelistener () {@ overridepublic void oncheckedchanged (radiogroup group, int checkedid) {// todo auto-generated method stubif (checkedid = m_radio2.getid () {displaytoast ("correct answer: "+ m_radio2.gettext () +", congratulations! Correct answer! ");} Else {displaytoast (" Please note that the answer is incorrect! ");}});

 

I learned p69 today and continue to work on it.

 

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.