活动(Activity)是最容易吸引用户的地方,它是一种可以包含用户界面的组件,主要用于和用户进行交互。
- package com.example.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
- protected void onCreate(Bundle savedInstanceState) {
- //------------------------------------------
- // 以下默认生成
- super.onCreate(savedInstanceState);
- setContentView(R.layout.first_layout);
-
- // ---------------------------------------
- // 通过ID文件中取得类实例
- Button button1 = (Button) findViewById(R.id.button_1);
- button1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(Intent.ACTION_DIAL);
- intent.setData(Uri.parse("tel:10086"));
- startActivity(intent);
- }
- });
-
- /*
- // --------------------------------
- // 调用系统的浏览器来打开这个网页
- button1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setData(Uri.parse("http://www.baidu.com"));
- startActivity(intent);
- }
- });
- */
-
- /*
- // --------------------------------
- // 匹配的类别与action自动跳转到指定activity
- button1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // AndroidManifest.xml指明的action name
- Intent intent = new Intent("com.example.activitytest.ACTION_START");
- // AndroidManifest.xml指明的Category name
- intent.addCategory("com.example.activitytest.MY_CATEGORY");
- startActivity(intent);
- }
- });
- */
- /*
- // -----------------------------------------------
- // 隐式intent使用方法,在AndroidManifest.xml中指明跳转条件
- button1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent("com.example.activitytest.ACTION_START");
- startActivity(intent);
- }
- });
- */
- /*
- // -----------------------------------------------
- // 显式intent使用方法,指明转移activity类名
- button1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v)
- {
- Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
- startActivity(intent);
- }
- }
- );
- */
- /*
- // -----------------------------------------------
- // Toast提醒工具使用方法
- button1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Toast.makeText(FirstActivity.this, "You clicked Button 1",
- Toast.LENGTH_SHORT).show();
- }
- });
- */
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- //return super.onOptionsItemSelected(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;
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- //return super.onCreateOptionsMenu(menu);
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
注册在 “AndroidManifest.xml”中进行,加入如下红字标记两句,表示主活动界面,android studio中会自动的进行注册声明。

