1, Create and load layouts
app/src/main/res/new/directory, create a directory named layout, and right-click / layout resource file/
Add button code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/button_1"//Define an id in xml with +. No need for reference+ android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button_1" /> </LinearLayout>
Add in onCreate() in firstactivity
public class firstactivity extends AppCompatActivity { @Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first_layout); } }
Two
Register in the Android manifest file
Please refer to the first line of code for details of main activities to be configured, and add the label of < intent Filter > in < activity >
Source code after modification
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="activitytest.example.com.activitytest"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".firstactivity" android:label="This is firstactivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Three
Using toast in an activity
To define a starting point, use the previous button to add code to the onCreate () method
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first_layout); Button button1 = (Button) findViewById(R.id.button_1);//Get the elements defined in the layout file and convert them to a BUtton object button1.setOnClickListener(new View.OnClickListener(){//Register a listener @Override public void onClick(View v){ Toast.makeText(firstactivity.this,"You clicked Button 1", Toast.LENGTH_SHORT).show();//The first parameter is the context required by Toast. The second parameter is the real content. The third parameter is the display time } }); }
Using menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/add_item" android:title="ADD"/> <item android:id="@+id/remove_item"//Specific labels android:title="REMOVE"/> </menu>
IV. destruction activities
5. Use explicit and implicit intent
Fireactivity code (three, four, five)
package activitytest.example.com.activitytest; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.Toast; public class firstactivity extends AppCompatActivity { @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub getMenuInflater().inflate(R.menu.main, menu); return true; } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first_layout); Button button1 = (Button) findViewById(R.id.button_1);//Get the elements defined in the layout file and convert them to a BUtton object button1.setOnClickListener(new View.OnClickListener(){//Register a listener @Override public void onClick(View v){ Toast.makeText(firstactivity.this,"You clicked Button 1", // finish(); / / destroy the activity Toast.LENGTH_SHORT).show();//The first parameter is the context required by Toast. The second parameter is the real content. The third parameter is the display time //Intent intent = new Intent(firstactivity.this,SecondActivity.class); / / first we build an intent, the first as the context, and the second as the target activity (explicit intent) // Intent intent= new Intent(Intent.ACTION_VIEW); //intent.addCategory("activitytest.example.com.activitytest.MY_CATEGORY"); //intent.setData(Uri.parse("http://www.baidu.com")); //startActivity(intent); / / open the second one based on the first one and execute the intent } }); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()){ case R.id.add_item: Toast.makeText(this, "you clicked ADD", Toast.LENGTH_SHORT).show(); break; case R.id.remove_item: Toast.makeText(this, "you clicked REMOVE", Toast.LENGTH_SHORT).show(); break; default: } return true; }}