正确代码 = =
1.Testactivity1.java
1 package com.hanqi.myapplication; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.util.Log; 7 import android.view.View; 8 import android.widget.EditText; 9 import android.widget.Toast; 10 11 //1.继承Activity 12 /** 13 * Created by lenovo on 2016/4/22. 14 */ 15 public class TestActivity1 extends Activity { 16 17 //成员变量 18 EditText et1; 19 EditText et2; 20 EditText et3; 21 //2.重写onCreate(),关联Layout文件 22 //onCreate()是一个回调方法:在满足特定条件下自动调用的方法;方法名一般on开头 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 28 //关联 29 setContentView(R.layout.message_relativelayout); 30 //初始化工作 31 //获取Layout文件中定义的组件 32 33 et1=(EditText)findViewById(R.id.et1); 34 et2=(EditText)findViewById(R.id.et2); 35 et3=(EditText)findViewById(R.id.et3); 36 Log.e("TAG","onCreat()被调用"); 37 38 //得到意图 39 Intent intent = getIntent(); 40 String strname = intent.getStringExtra("name"); 41 String strname1 = intent.getStringExtra("name1"); 42 //intent.getExtras(); 43 Log.e("TAG","意图传递的数据="+strname); 44 Log.e("TAG","意图传递的数据1="+strname1); 45 // if(savedInstanceState!=null&&!savedInstanceState.isEmpty()) 46 // { 47 // et1.setText(savedInstanceState.getString("et1")+"恢复之后的"); 48 // et2.setText(savedInstanceState.getString("et2")); 49 // et3.setText(savedInstanceState.getString("et3")); 50 // } 51 } 52 53 //保存状态 54 @Override 55 protected void onSaveInstanceState(Bundle outState) { 56 super.onSaveInstanceState(outState); 57 Log.e("TAG", "保存应用状态"); 58 59 outState.putString("et1", et1.getText().toString()); 60 outState.putString("et1",et2.getText().toString()); 61 outState.putString("et1",et3.getText().toString()); 62 } 63 64 //恢复状态 65 @Override 66 protected void onRestoreInstanceState(Bundle savedInstanceState) { 67 super.onRestoreInstanceState(savedInstanceState); 68 Log.e("TAG", "恢复应用状态"); 69 70 et1.setText(savedInstanceState.getString("et1")); 71 et2.setText(savedInstanceState.getString("et2")); 72 et3.setText(savedInstanceState.getString("et3")); 73 74 } 75 76 //启动 77 @Override 78 protected void onStart() { 79 super.onStart(); 80 Log.e("TAG","onStart()被调用"); 81 } 82 //重启 83 @Override 84 protected void onRestart() { 85 super.onRestart(); 86 Log.e("TAG", "onRestart()被调用"); 87 } 88 //继续 89 @Override 90 protected void onResume() { 91 super.onResume(); 92 Log.e("TAG", "onResume()被调用"); 93 } 94 //暂停 95 @Override 96 protected void onPause() { 97 super.onPause(); 98 Log.e("TAG", "onPause()被调用"); 99 } 100 //停止 101 @Override 102 protected void onStop() { 103 super.onStop(); 104 Log.e("TAG", "onStop()被调用"); 105 } 106 //销毁 107 @Override 108 protected void onDestroy() { 109 super.onDestroy(); 110 Log.e("TAG", "onDestroy()被调用"); 111 } 112 //点击事件方法 113 public void bt_OnClick(View v) 114 { 115 //显示提示信息 116 //方法链 117 Toast.makeText(TestActivity1.this, "消息发送成功", Toast.LENGTH_SHORT).show(); 118 } 119 public void close_OnClick(View v) 120 { 121 //关闭应用 122 finish(); 123 } 124 }
2.MainActivity.java
1 package com.hanqi.myapplication; 2 3 import android.content.ComponentName; 4 import android.content.Intent; 5 import android.net.Uri; 6 import android.os.Bundle; 7 import android.support.v7.app.AppCompatActivity; 8 import android.util.Log; 9 import android.view.View; 10 import android.widget.Button; 11 12 public class MainActivity extends AppCompatActivity { 13 14 //回调方法 (on开头的方法都是) 15 //在创建时自动调用 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 //调用父类的回调方法 19 super.onCreate(savedInstanceState); 20 //设置内容视图文件 21 //建立Activity和Layout文件之间的关联 22 setContentView(R.layout.test_linearlayout); 23 //1.获得这个组件 24 Button bt2 = (Button)findViewById(R.id.button2); 25 //2.操作这个组件 26 // bt2.setText("新按钮"); 27 //日志输出 28 System.out.print("日志输出=应用开始运行"); 29 Log.v("HANQI", "Verbose级别的日志信息"); 30 Log.d("HANQI", "Debug级别的日志信息"); 31 Log.i("HANQI", "Info级别的日志信息"); 32 Log.w("HANQI", "Warning级别的日志信息"); 33 Log.e("HANQI", "Error级别的日志信息"); 34 } 35 public void login_onClick(View v) 36 { 37 //打开新的Activity 38 //1.创建意图 显式意图 39 Intent intent = new Intent(); 40 //定义显式意图 41 ComponentName componentName = new ComponentName(this,TestActivity1.class); 42 intent.setComponent(componentName); 43 intent.putExtra("name", "意图传递的值"); 44 intent.putExtra("name1", "意图传递的值1"); 45 46 //2.发起意图 47 startActivity(intent); 48 } 49 public void bt2_onClick(View v) 50 { 51 //发起隐式意图 52 //打开拨打电话的界面 53 //系统已经预先定义了常用功能的Action的字符串常量 54 Intent intent2 = new Intent(Intent.ACTION_CALL); 55 //intent2.setAction(Intent.ACTION_DIAL); 56 57 //构造Uri 58 Uri uri = Uri.parse("tel:110"); 59 60 //intent2.addCategory(""); 61 62 //设置data 63 intent2.setData(uri); 64 65 //intent2.setType(""); 66 //intent2.setDataAndType(uri,""); 67 68 try 69 { 70 startActivity(intent2); 71 } 72 catch (Exception e){ 73 e.printStackTrace(); 74 } 75 } 76 77 public void bt3_onClick(View v) 78 { 79 //返回桌面 80 Intent intent3 = new Intent(Intent.ACTION_MAIN); 81 82 intent3.addCategory(Intent.CATEGORY_HOME); 83 84 startActivity(intent3); 85 } 86 public void bt4_onClick(View v) 87 { 88 //定义隐式意图 89 Intent intent4 = new Intent("com.hanqi.action.test1"); 90 //如果不主动添加Category,默认给Category加上"android.intent.category.DEFAULT" 91 //主动添加Category 92 intent4.addCategory("com.hanqi.category.test1"); 93 startActivity(intent4); 94 } 95 }
3.Android Mani fest.xml
1 package com.hanqi.myapplication; 2 3 import android.content.ComponentName; 4 import android.content.Intent; 5 import android.net.Uri; 6 import android.os.Bundle; 7 import android.support.v7.app.AppCompatActivity; 8 import android.util.Log; 9 import android.view.View; 10 import android.widget.Button; 11 12 public class MainActivity extends AppCompatActivity { 13 14 //回调方法 (on开头的方法都是) 15 //在创建时自动调用 16 @Override 17 protected void onCreate(Bundle savedInstanceState) { 18 //调用父类的回调方法 19 super.onCreate(savedInstanceState); 20 //设置内容视图文件 21 //建立Activity和Layout文件之间的关联 22 setContentView(R.layout.test_linearlayout); 23 //1.获得这个组件 24 Button bt2 = (Button)findViewById(R.id.button2); 25 //2.操作这个组件 26 // bt2.setText("新按钮"); 27 //日志输出 28 System.out.print("日志输出=应用开始运行"); 29 Log.v("HANQI", "Verbose级别的日志信息"); 30 Log.d("HANQI", "Debug级别的日志信息"); 31 Log.i("HANQI", "Info级别的日志信息"); 32 Log.w("HANQI", "Warning级别的日志信息"); 33 Log.e("HANQI", "Error级别的日志信息"); 34 } 35 public void login_onClick(View v) 36 { 37 //打开新的Activity 38 //1.创建意图 显式意图 39 Intent intent = new Intent(); 40 //定义显式意图 41 ComponentName componentName = new ComponentName(this,TestActivity1.class); 42 intent.setComponent(componentName); 43 intent.putExtra("name", "意图传递的值"); 44 intent.putExtra("name1", "意图传递的值1"); 45 46 //2.发起意图 47 startActivity(intent); 48 } 49 public void bt2_onClick(View v) 50 { 51 //发起隐式意图 52 //打开拨打电话的界面 53 //系统已经预先定义了常用功能的Action的字符串常量 54 Intent intent2 = new Intent(Intent.ACTION_CALL); 55 //intent2.setAction(Intent.ACTION_DIAL); 56 57 //构造Uri 58 Uri uri = Uri.parse("tel:110"); 59 60 //intent2.addCategory(""); 61 62 //设置data 63 intent2.setData(uri); 64 65 //intent2.setType(""); 66 //intent2.setDataAndType(uri,""); 67 68 try 69 { 70 startActivity(intent2); 71 } 72 catch (Exception e){ 73 e.printStackTrace(); 74 } 75 } 76 77 public void bt3_onClick(View v) 78 { 79 //返回桌面 80 Intent intent3 = new Intent(Intent.ACTION_MAIN); 81 82 intent3.addCategory(Intent.CATEGORY_HOME); 83 84 startActivity(intent3); 85 } 86 public void bt4_onClick(View v) 87 { 88 //定义隐式意图 89 Intent intent4 = new Intent("com.hanqi.action.test1"); 90 //如果不主动添加Category,默认给Category加上"android.intent.category.DEFAULT" 91 //主动添加Category 92 intent4.addCategory("com.hanqi.category.test1"); 93 startActivity(intent4); 94 } 95 }
4.Test linearlayout.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:padding="10dp"> 7 8 <LinearLayout 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content"> 11 <TextView 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="用户名:"/> 15 <EditText 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:hint="请输入用户名"/> 19 </LinearLayout> 20 <LinearLayout 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content"> 23 <TextView 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:text="密码:"/> 27 <EditText 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content" 30 android:hint="请输入密码" 31 android:inputType="textPassword" 32 android:maxLength="6"/> 33 </LinearLayout> 34 35 <LinearLayout 36 android:layout_width="match_parent" 37 android:layout_height="wrap_content"> 38 <Button 39 android:layout_width="0dp" 40 android:layout_height="wrap_content" 41 android:text="显式意图" 42 android:id="@+id/button" 43 android:layout_weight="1" 44 android:onClick="login_onClick"/> 45 46 <Button 47 android:layout_width="0dp" 48 android:layout_height="wrap_content" 49 android:text="隐式意图" 50 android:id="@+id/button3" 51 android:layout_weight="1" 52 android:onClick="bt2_onClick"/> 53 <Button 54 android:layout_width="0dp" 55 android:layout_height="wrap_content" 56 android:text="返回桌面" 57 android:id="@+id/button4" 58 android:layout_weight="1" 59 android:onClick="bt3_onClick"/> 60 61 <Button 62 android:layout_width="wrap_content" 63 android:layout_height="wrap_content" 64 android:text="New Button" 65 android:id="@+id/button2" 66 android:layout_gravity="center" 67 android:textSize="20sp" 68 android:textColor="@color/colorPrimary" 69 android:visibility="gone"/> 70 </LinearLayout> 71 <Button 72 android:layout_width="match_parent" 73 android:layout_height="wrap_content" 74 android:text="隐式意图打开Activity" 75 android:onClick="bt4_onClick"/> 76 </LinearLayout>
5.TestAcivity2.java
1 package com.hanqi.myapplication; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.util.Log; 7 import android.view.View; 8 import android.widget.EditText; 9 import android.widget.Toast; 10 11 //1.继承Activity 12 13 /** 14 * Created by lenovo on 2016/4/22. 15 */ 16 public class TestActivity2 extends Activity{ 17 18 //成员变量 19 EditText et1; 20 EditText et2; 21 EditText et3; 22 //2.重写onCreate(),关联Layout文件 23 //onCreate()是一个回调方法:在满足特定条件下自动调用的方法;方法名一般on开头 24 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 29 //关联 30 setContentView(R.layout.message_relativelayout); 31 //初始化工作 32 //获取Layout文件中定义的组件 33 34 et1=(EditText)findViewById(R.id.et1); 35 et2=(EditText)findViewById(R.id.et2); 36 et3=(EditText)findViewById(R.id.et3); 37 Log.e("TAG","onCreat()被调用"); 38 39 //得到意图 40 Intent intent = getIntent(); 41 String strname = intent.getStringExtra("name"); 42 String strname1 = intent.getStringExtra("name1"); 43 //intent.getExtras(); 44 Log.e("TAG","意图传递的数据="+strname); 45 Log.e("TAG","意图传递的数据1="+strname1); 46 // if(savedInstanceState!=null&&!savedInstanceState.isEmpty()) 47 // { 48 // et1.setText(savedInstanceState.getString("et1")+"恢复之后的"); 49 // et2.setText(savedInstanceState.getString("et2")); 50 // et3.setText(savedInstanceState.getString("et3")); 51 // } 52 } 53 54 //保存状态 55 @Override 56 protected void onSaveInstanceState(Bundle outState) { 57 super.onSaveInstanceState(outState); 58 Log.e("TAG", "保存应用状态"); 59 60 outState.putString("et1", et1.getText().toString()); 61 outState.putString("et1",et2.getText().toString()); 62 outState.putString("et1",et3.getText().toString()); 63 } 64 65 //恢复状态 66 @Override 67 protected void onRestoreInstanceState(Bundle savedInstanceState) { 68 super.onRestoreInstanceState(savedInstanceState); 69 Log.e("TAG", "恢复应用状态"); 70 71 et1.setText(savedInstanceState.getString("et1")+"恢复之后的"); 72 et2.setText(savedInstanceState.getString("et2")); 73 et3.setText(savedInstanceState.getString("et3")); 74 75 } 76 77 //启动 78 @Override 79 protected void onStart() { 80 super.onStart(); 81 Log.e("TAG","onStart()被调用"); 82 } 83 //重启 84 @Override 85 protected void onRestart() { 86 super.onRestart(); 87 Log.e("TAG", "onRestart()被调用"); 88 } 89 //继续 90 @Override 91 protected void onResume() { 92 super.onResume(); 93 Log.e("TAG", "onResume()被调用"); 94 } 95 //暂停 96 @Override 97 protected void onPause() { 98 super.onPause(); 99 Log.e("TAG", "onPause()被调用"); 100 } 101 //停止 102 @Override 103 protected void onStop() { 104 super.onStop(); 105 Log.e("TAG", "onStop()被调用"); 106 } 107 //销毁 108 @Override 109 protected void onDestroy() { 110 super.onDestroy(); 111 Log.e("TAG", "onDestroy()被调用"); 112 } 113 //点击事件方法 114 public void bt_OnClick(View v) 115 { 116 //显示提示信息 117 //方法链 118 Toast.makeText(TestActivity2.this, "消息发送成功", Toast.LENGTH_SHORT).show(); 119 } 120 public void close_OnClick(View v) 121 { 122 //关闭应用 123 finish(); 124 } 125 }