第一行代码Android学习:第一部分
主要涉及到Activity的基本用法和Activity之间的跳转
1.AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.dyhdm_02_activitytest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.dyhdm_02_activitytest.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.dyhdm_02_activitytest.SecondActivity" android:label="@string/title_activity_second" > <intent-filter> <action android:name="com.example.dyhdm_02_activitytest.Action_START" /> <category android:name="com.example.dyhdm_02_activitytest.MY_CATEGORY" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name="com.example.dyhdm_02_activitytest.ThirdActivity" android:label="@string/title_activity_third" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" /> </intent-filter> </activity> </application> </manifest> 2.activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <Button android:id="@+id/bt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="显式" /> <Button android:id="@+id/bt2" android:layout_below="@id/bt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="隐式1" /> <Button android:id="@+id/bt3" android:layout_below="@id/bt2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="隐式2" /> <Button android:id="@+id/bt4" android:layout_below="@id/bt3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="打开浏览器" /> <Button android:id="@+id/bt5" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/bt4" android:text="打开拨号界面" /> <Button android:id="@+id/bt6" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/bt5" android:text="显示启动并传递数据" /> <Button android:id="@+id/bt7" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/bt6" android:text="显示启动并返回数据" /> </RelativeLayout> 3.activity_second.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SecondActivity" > <Button android:id="@+id/bt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="关闭页面" /> <Button android:id="@+id/bt2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/bt1" android:text="显示传递的数据/返回数据给上一个页面" /> <TextView android:id="@+id/tv1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/bt2" /> </RelativeLayout> 4.activity_third.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ThirdActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> </RelativeLayout> 5.activity_main.xml <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/add_settings" android:orderInCategory="100" android:showAsAction="never" android:title="add"/> <item android:id="@+id/remove_settings" android:orderInCategory="100" android:showAsAction="never" android:title="remove"/> </menu> 6.MainActivity.java package com.example.dyhdm_02_activitytest; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button bt1; private Button bt4; private Button bt3; private Button bt2; private Button bt5; private Button bt6; private Button bt7; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); bt1 = (Button) findViewById(R.id.bt1); bt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 显式 startActivity(new Intent(MainActivity.this, SecondActivity.class)); } }); bt2 = (Button) findViewById(R.id.bt2); bt2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 隐式1 // 在AndroidManifest.xml为SecondActivity进行注册中加入如下代码 // <intent-filter> // <action // android:name="com.example.dyhdm_02_activitytest.Action_START" // /> // // <category android:name="android.intent.category.DEFAULT" /> // </intent-filter> startActivity(new Intent( "com.example.dyhdm_02_activitytest.Action_START")); } }); bt3 = (Button) findViewById(R.id.bt3); bt3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 隐式2 // 每个Activity中只能指定一个action,却能指定多个category // 在AndroidManifest.xml为SecondActivity进行注册中加入如下代码 // <intent-filter> // <action // android:name="com.example.dyhdm_02_activitytest.Action_START" // /> // // <category // android:name="com.example.dyhdm_02_activitytest.MY_CATEGORY" // /> // <category android:name="android.intent.category.DEFAULT" /> // </intent-filter> startActivity(new Intent( "com.example.dyhdm_02_activitytest.Action_START") .addCategory("com.example.dyhdm_02_activitytest.MY_CATEGORY")); } }); bt4 = (Button) findViewById(R.id.bt4); bt4.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 使用隐式intent打开浏览器 // public static final String ACTION_VIEW = // "android.intent.action.VIEW"; startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri .parse("http://www.baidu.com"))); } }); bt5 = (Button) findViewById(R.id.bt5); bt5.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 使用隐式intent打开系统拨号界面 // public static final String ACTION_DIAL = // "android.intent.action.DIAL"; startActivity(new Intent(Intent.ACTION_DIAL).setData(Uri .parse("tel:10010"))); } }); bt6 = (Button) findViewById(R.id.bt6); bt6.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this, SecondActivity.class).putExtra("data", "HelloWorld")); } }); bt7 = (Button) findViewById(R.id.bt7); bt7.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 1:请求码 // @param requestCode If >= 0, this code will be returned in // onActivityResult() when the activity exits. startActivityForResult(new Intent(MainActivity.this, SecondActivity.class), 1); } }); } /** * 接受返回的数据 重载方法 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case 1: if (resultCode == RESULT_OK) { Toast.makeText(MainActivity.this, data.getStringExtra("data"), Toast.LENGTH_SHORT).show(); } break; default: break; } } /** * 在Activity中使用Menu 重载方法 */ @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } /** * Menu子项的点击监听 重载方法 */ @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.add_settings: Toast.makeText(MainActivity.this, "add_settings", Toast.LENGTH_SHORT).show(); break; case R.id.remove_settings: Toast.makeText(MainActivity.this, "remove_settings", Toast.LENGTH_SHORT).show(); break; default: break; } return true; } } 7.SecondActivity.java package com.example.dyhdm_02_activitytest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class SecondActivity extends Activity { private Button bt1; private Button bt2; private TextView tv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); bt1 = (Button) findViewById(R.id.bt1); bt1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); bt2 = (Button) findViewById(R.id.bt2); tv1 = (TextView) findViewById(R.id.tv1); bt2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (getIntent().getStringExtra("data") != null) { tv1.setText(getIntent().getStringExtra("data")); } else { // RESULT_OK:返回码 // @param resultCode The result code to propagate back to // the originating // activity, often RESULT_CANCELED or RESULT_OK // Standard activity result: operation canceled. // public static final int RESULT_CANCELED = 0; // Standard activity result: operation succeeded. // public static final int RESULT_OK = -1; setResult(RESULT_OK, new Intent().putExtra("data", "HelloBoy")); finish(); } } }); } /** * 按下返回按钮 重载方法 */ @Override public void onBackPressed() { if (getIntent().getStringExtra("data") != null) { finish(); } else { setResult(RESULT_OK, new Intent().putExtra("data", "HelloBoy")); finish(); } } } 8.ThirdActivity.java package com.example.dyhdm_02_activitytest; import android.app.Activity; import android.os.Bundle; import android.view.Menu; /** * * TODO 添加下列代码,配置了一个可以响应打开网页的intent * * <activity android:name="com.example.dyhdm_02_activitytest.ThirdActivity" android:label="@string/title_activity_third" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" /> </intent-filter> </activity> * @author zza * @data: 2016-8-12 下午3:40:45 */ public class ThirdActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_third); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_third, menu); return true; } }代码下载地址