(iv) understanding and use of the context in Android
Source: Internet
Author: User
<span id="Label3"></p>I. The role of the context<p><p>Context can be used to access global Resources.</p></p> <blockquote> <blockquote> <pre>public class Mainactivity extends Activity {<br> Private TextView tv;<br> @Override<br> protected void OnCreate (Bundle Savedinstancestate) {<br> Super.oncreate (savedinstancestate);<br> Setcontentview (r.layout.activity_main);</pre> <pre><br> TV = new TextView (mainactivity.this);<br> Tv.settext ("qian long");<br> Tv.settext (r.string.hello_world);<br> Tv.settext (getresources (). getText (r.string.hello_world)); Accessing Global Resources<br> Setcontentview (tv);</pre> <pre><br> ImageView iv=new ImageView (this);<br> Iv.setimageresource (r.mipmap.ic_launcher); Accessing Global Resources<br> Setcontentview (iv);<br><br> }<br> }</pre> </blockquote> </blockquote>second, the use of application<p><p>Public layout Files:</p></p><pre><pre><?xml version= "1.0" encoding= "utf-8"?><br><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"<br> android:orientation= "vertical" android:layout_width= "match_parent"<br> android:layout_height= "match_parent" ><br> <textview<br> Android:layout_width= "wrap_content"<br> android:layout_height= "wrap_content"<br> Android:id= "@+id/textview"/><br><br> <edittext<br> Android:layout_width= "wrap_content"<br> android:layout_height= "wrap_content"<br> Android:id= "@+id/edittext"<br> /><br><br> <button<br> Android:layout_width= "wrap_content"<br> android:layout_height= "wrap_content"<br> android:text= "save"<br> Android:id= "@+id/btnsave"/><br><br></LinearLayout></pre></pre><pre><pre><?xml version= "1.0" encoding= "utf-8"?></pre></pre><pre>Androidmanifest.xml file:<br><manifest xmlns:android= "http://schemas.android.com/apk/res/android"<br>Package= "com.example.shiyanshi.learncontext" ><br><br><application<br>Android:name= ". App "<br>android:allowbackup= "true"<br>android:icon= "@mipmap/ic_launcher"<br>Android:label= "@string/app_name"<br>Android:theme= "@style/apptheme";?<br><activity<br>Android:name= ". Mainactivity "<br>Android:label= "learncontext" > <!--installed application name--<br><intent-filter> <!--specify that the activity is the activity--> to start<br><action android:name= "android.intent.action.MAIN"/><br><category android:name= "android.intent.category.LAUNCHER"/><br></intent-filter><br></activity><br><activity<br>Android:name= ". Main2aty "<br>Android:label= "Main2" ><br><intent-filter><br><action android:name= "android.intent.action.MAIN"/> <!--must be specified as MAIN to install two applications--<br><category android:name= "android.intent.category.LAUNCHER"/><br></intent-filter><br><br></activity><br></application><br><br></manifest></pre><p><p>1. Create a new application class in which to set public resources that need to be accessed by the program, where a string</p></p><pre><pre>public class App extends application {<br> Private String Stringdata= "default";<br><br> public void Setstringdata (String Stringdata) {<br> This.stringdata = stringdata;<br> }<br><br> Public String Getstringdata () {<br> Return stringdata;<br> }<br>}</pre></pre><pre><pre>2. Then set the application Name property to the new class in the Androidmanifest.xml file</pre></pre><pre><pre><application<br>Android:name= ". APP "></pre></pre><pre><pre>3. Create a new activity and make the activity a startup activity (that is, install two boot icons in the system), Note To set the Activity's properties in the Androidmanifest.xml file as in the Main2aty above,</pre></pre><pre><pre>Note that the name of the action in the filter must be set to Android.intent.action.MAIN and the category name must be set to Android.intent.category.LAUNCHER so that only two boot icons are displayed in the System.<br></pre></pre><pre><pre>public class Main2aty extends Activity {<br> Private TextView TextView;<br> Private EditText EditText;<br><br> @Override<br> protected void OnCreate (Bundle Savedinstancestate) {<br> Super.oncreate (savedinstancestate);<br> Setcontentview (r.layout.activity_common);<br> textview= (textView) Findviewbyid (r.id.textview);<br> edittext= (editText) Findviewbyid (r.id.edittext);<br> Textview.settext (getapp (). Getstringdata ());<br> Findviewbyid (r.id.btnsave). setonclicklistener (new view.onclicklistener () {<br> @Override<br> public void OnClick (view View) {<br> Getapp (). setstringdata (edittext.gettext (). toString ());<br> Textview.settext (getapp (). Getstringdata ());<br> }<br> });<br> }<br><br> APP Getapp ()<br> {<br> return (APP) Getapplicationcontext ();<br> }<br>}</pre></pre><pre><pre>4. Main Activity</pre></pre><pre><pre>public class Mainactivity extends Activity {<br> Private TextView TextView;<br> Private EditText EditText;<br> @Override<br> protected void OnCreate (Bundle Savedinstancestate) {<br> Super.oncreate (savedinstancestate);</pre></pre><pre><pre> Setcontentview (r.layout.activity_common);<br> textview= (textView) Findviewbyid (r.id.textview);<br> edittext= (editText) Findviewbyid (r.id.edittext);<br> Textview.settext (getapp (). Getstringdata ());<br> Findviewbyid (r.id.btnsave). setonclicklistener (new view.onclicklistener () {<br> @Override<br> public void OnClick (view View) {<br> Getapp (). setstringdata (edittext.gettext (). toString ());<br> Textview.settext (getapp (). Getstringdata ());<br> }<br> });<br> }<br> APP Getapp ()<br> {<br> return (APP) Getapplicationcontext ();<br> }</pre></pre><pre><pre>}</pre></pre><pre><pre>5. Note that in 3,4, you can access the global resource by Getapplicationcontext () to get the subclass of the newly created application, and then call the methods in it.</pre></pre><p><p></p></p>three, the life cycle of application<p><p>The overloaded functions of application are as follows, OnCreate are executed before the activity oncreate, and each activity executes application OnCreate before Execution. therefore, it is convenient to set the global initialization information in it.</p></p><p><p>Onterminate,onlowmemory,ontrimmemory are generally not executed.</p></p><pre><pre>@Override<br>public void OnCreate () {<br> Super.oncreate ();<br> System.out.println ("App onCreate");<br>}<br><br>@Override<br>public void Onterminate () {<br> Super.onterminate ();<br>}<br><br>@Override<br>public void Onlowmemory () {<br> Super.onlowmemory ();<br>}<br><br>@Override<br>public void Ontrimmemory (int. Level) {<br> Super.ontrimmemory (level);<br>}</pre></pre><p><p>(iv) understanding and use of the context in Android</p></p></span>
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