您的位置:首页 > 移动开发 > Android开发

Android-activity-intent

2020-01-15 11:34 639 查看
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,TextActivity.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_DIAL);
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         startActivity(intent2);
68     }
69
70     public void bt3_onClick(View v)
71     {
72         //返回桌面
73         Intent intent3 = new Intent(Intent.ACTION_MAIN);
74         intent3.addCategory(Intent.CATEGORY_HOME);
75         startActivity(intent3);
76     }
77 }
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 TextActivity 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(TextActivity.this, "消息发送成功", Toast.LENGTH_SHORT).show();
118     }
119     public void close_OnClick(View v)
120     {
121         //关闭应用
122         finish();
123     }
124 }
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:background="@drawable/anniu05"
70         android:visibility="gone"/>
71 </LinearLayout>
72 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3     package="com.hanqi.myapplication">
4
5     <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
6
7     <application
8         android:allowBackup="true"
9         android:icon="@mipmap/ic_launcher"
10         android:label="@string/app_name"
11         android:supportsRtl="true"
12         android:theme="@style/AppTheme">
13
14         <activity android:name=".MainActivity">
15             <intent-filter>
16                 <action android:name="android.intent.action.MAIN" />
17
18                 <category android:name="android.intent.category.LAUNCHER" />
19             </intent-filter>
20         </activity>
21         <activity android:name=".TextActivity">
22             <intent-filter>
23                 <action android:name="android.intent.action.MAIN" />
24
25                 <category android:name="android.intent.category.LAUNCHER" />
26             </intent-filter>
27         </activity>
28
29     </application>
30
31 </manifest>

转载于:https://www.cnblogs.com/TENOKAWA/p/5432359.html

  • 点赞
  • 收藏
  • 分享
  • 文章举报
banglun5623 发布了0 篇原创文章 · 获赞 0 · 访问量 197 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: