2017-09-08
Write in front
The new school year to start a project ready to graduate is AR new field but to do it on an Android phone so Java and Android are essential
Try to study Android.
Activity
Setcontentview () method to load a layout for the current activity
Toast in the program can provide some short messages to the user, these messages will automatically disappear in the future, you need to define a popup Toast trigger point. Create a Toast object from the static method Maketext (), then call the show ()maketext () parameter, the first is the context, the second is the text content of the toast display, and the third argument is the duration of the toast reality
Findviewbyid () Gets the element defined in the layout file, which returns a View object, so we need to move down to another object
Setonclicklistener Registers a listener for the button and executes the onclick () method in the listener when the button is clicked
@Override protected void OnCreate (Bundle savedinstancestate) { super .oncreate (savedinstancestate); Setcontentview (r.layout.first_layout); Button button1 = (Button) Findviewbyid (r.id.button_1); Button1.setonclicklistener ( new View.onclicklistener () {@Override public void OnClick (View v) {toast.maketext (firstactivity. this , "You ordered Button1" ,toast.length_long). Show (); } }); }
Menu
Create a menu folder in Rec, and then menu resource file
<item> Add menu items
Rewrite Oncreateoptionsmenu (), the Getmenuinflater () method to get the Menuinflater object, and then call its Inflater () method to create a menu for the current activity
The inflate () method receives two parameters, the first parameter specifies which resource file we use to create the menu, and the second parameter specifies which menu item to add to
Public Boolean Oncreateoptionsmenu (Menu menu) { //return Super.oncreateoptionsmenu (menu); Getmenuinflater (). Inflate (R.menu.main,menu); return true ; }
Defining Menu Response Events
In the onoptionsitemselected () method, call Item.getitemid () to determine which menu item we clicked on, and then add each menu item to its own logical handling
Finish () can destroy the current activity
Intent is an important way to interact with components in an Android program, not only to indicate what actions the current component wants to perform, but also to pass data between different components. Intent have explicit and implicit
About an article that will intent and intentfilter good
A better article.
Intent (Context Packagecontext, class<?> CLS) , the first parameter of the context requires that a start activity is provided, and the second Class is the target activity that you want to start. The intent of the coarse intent can be constructed by this construct.
The activity class provides a StartActivity () method that is specifically used to initiate the activity, which receives a intent parameter
Implicit intent
Instead of explicitly pointing out which activity we want to start, we specify a series of more abstract actions and category information, which we then refer to the system to analyze the intent and help us find the right activity to start.
The intent parsing mechanism is primarily to find the matching intent by locating all intentfilter and the intent defined therein in the androidmanifest.xml.
In Androidmanifest.xml
<activity android:name= ". Secondactivity "> <intent-filter> <action android:name=" Com.example.rebeccafan.myapplication.ACTION_START "/> <category android:name=" Android.intent.category.DEFAULT "/> </intent-filter> </activity>
Public void OnClick (View v) { ///Toast.maketext (firstactivity.this, "You ordered Button1", Toast.length_long). Show (); // Intent Intent = new Intent (firstactivity.this,secondactivity.class); Intent Intent =new Intent ("Com.example.rebeccafan.myapplicaiton.ACTION_START"); StartActivity (intent); }
Using implicit intent, we can not only start the activities within the program, but also start the activities of other programs
public void OnClick (View v) { // toast.maketext (Firstactivity.this," You ordered Button1 ", Toast.length_long). Show (); // intent Intent = new Intent ( Firstactivity.this,secondactivity.class); // intent Intent =new Intent (" Com.example.rebeccafan.myapplicaiton.ACTION_START "); Intent Intent = new Intent (Intent.action_view); Intent.setdata (Uri.parse ( "http://www.pornhub.com"
The action specified by intent is intent.action_view, an action built into the Android system with a constant value of Android.intent.action.VIEW
<activity android:name= ". Thirdactivity "> <intent-filter> <action android:name=" Android.intent.action.VIEW "/> <category android:name= "Android.intent.category.DEFAULT"/> <data android:scheme= "http"/> </intent-filter></activity>
The protocol that specifies the data through Android:scheme must be the HTTP protocol, so that thirdactivity should be able to respond to an open web page in the same way as a browser intent
Intent can also pass data when the activity is started
PutExtra () can put the data we want to pass in the intent, after launching another activity, just need to take this data out of intent to
Use the Getintent () method to get to the intent used to start secondactivity, and then call the Getstringextra () method to get the data passed
Layout
Andorid:id is to define a unique identifier for the current element, which can then be manipulated in code
In XML, define a id,@+id/id_name reference to an ID @id/id_name
andorid:layout_width Specifies the width of the current element match_parent means that the current element is the same as the parent element
andorid:layout_height Specifies the height of the current element wrap_content indicates that the current element contains exactly what is inside it.
Andorid:text Specifies the text content displayed in the element
1.1 Points to learn Java