Original:. Net moving Android 4.4 Daily notes (4)--button events and internationalization
We know that resources are registered to R.java we can read the components in the interface through R.java. As with our. NET, the component is read by ID
Knowledge Points:
- Reading components via R.java
- Mainactivity.java finding components by Findviewbyid method
- Using @string to find strings in layout, this is a basic acquaintance with the internationalization of our MVC, and we switch between Chinese and English through the resource reference of XML.
- Join Chinese and English localization
Double-click Fragment_main.xml Design view, drag a button and a TextView, and double-click into the XML code
Add Android:onclick, I also rely on guessing, program code has similarities, especially Java and JavaScript. But we're better off looking for help documents.
Input android:onclick= "Settextview"
So this is like JavaScript, there must be a Settextview method in the background to listen to this event
Open our Mainactivity.java Join method
Public void Settextview (View v) { TextView TV=(TextView) Findviewbyid (r.id.textview1); Tv.settext ("Click on the button I'm going to change!") "); }
Run to look at the effect, made a lable font settings like nothing to show off
Ok. Now let's see how to switch between the Chinese and English versions!
Our button name is buttons, when the mobile phone system is set to Chinese, it should be the words of the pushbutton, the English time display button
Now open res-----values----String.xml Add a property (here equals. NET internationalized Res resource file. XML with the corresponding health value)
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >helloworld</ string> <string name= "Hello_world" >hello world!</string> <string name= "Action_ Settings ">Settings</string> <string name=" Btn_name ">Button</string></resources>
<string name= "Btn_name" >Button</string>
In the values Create folder VALUES-ZH-RCN and copy the values under the String.xml to VALUES-ZH-RCN button to change buttons
<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >helloworld</ string> <string name= "Hello_world" >hello world!</string> <string name= "Action_settings" >Settings</string> <string name= "Btn_name" > Buttons </string></resources>
Back to layout, change the button's text from button to @string/btn_name.
In fact, the @ here is the meaning of R. That is R.string.btn_name we can open R.java and observe btn_name.
Run and open the version of the switching system run the Customlocale switch inside the simulator
. Net Moving Android 4.4 Daily notes (4)--button events and internationalization