Android-activity-intent

时间:2021-10-02 18:23:54
 package com.hanqi.myapplication;

 import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity { //回调方法 (on开头的方法都是)
//在创建时自动调用
@Override
protected void onCreate(Bundle savedInstanceState) {
//调用父类的回调方法
super.onCreate(savedInstanceState);
//设置内容视图文件
//建立Activity和Layout文件之间的关联
setContentView(R.layout.test_linearlayout);
//1.获得这个组件
Button bt2 = (Button)findViewById(R.id.button2);
//2.操作这个组件
// bt2.setText("新按钮");
//日志输出
System.out.print("日志输出=应用开始运行");
Log.v("HANQI", "Verbose级别的日志信息");
Log.d("HANQI", "Debug级别的日志信息");
Log.i("HANQI", "Info级别的日志信息");
Log.w("HANQI", "Warning级别的日志信息");
Log.e("HANQI", "Error级别的日志信息");
}
public void login_onClick(View v)
{
//打开新的Activity
//1.创建意图 显式意图
Intent intent = new Intent();
//定义显式意图
ComponentName componentName = new ComponentName(this,TextActivity.class);
intent.setComponent(componentName);
intent.putExtra("name", "意图传递的值");
intent.putExtra("name1", "意图传递的值1"); //2.发起意图
startActivity(intent);
}
public void bt2_onClick(View v)
{
//发起隐式意图
//打开拨打电话的界面
//系统已经预先定义了常用功能的Action的字符串常量
Intent intent2 = new Intent(Intent.ACTION_DIAL);
//intent2.setAction(Intent.ACTION_DIAL); //构造Uri
Uri uri = Uri.parse("tel:110"); //intent2.addCategory(""); //设置data
intent2.setData(uri); //intent2.setType("");
//intent2.setDataAndType(uri,"");
startActivity(intent2);
} public void bt3_onClick(View v)
{
//返回桌面
Intent intent3 = new Intent(Intent.ACTION_MAIN);
intent3.addCategory(Intent.CATEGORY_HOME);
startActivity(intent3);
}
}
 package com.hanqi.myapplication;

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; //1.继承Activity
/**
* Created by lenovo on 2016/4/22.
*/
public class TextActivity extends Activity { //成员变量
EditText et1;
EditText et2;
EditText et3;
//2.重写onCreate(),关联Layout文件
//onCreate()是一个回调方法:在满足特定条件下自动调用的方法;方法名一般on开头 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //关联
setContentView(R.layout.message_relativelayout);
//初始化工作
//获取Layout文件中定义的组件 et1=(EditText)findViewById(R.id.et1);
et2=(EditText)findViewById(R.id.et2);
et3=(EditText)findViewById(R.id.et3);
Log.e("TAG","onCreat()被调用"); //得到意图
Intent intent = getIntent();
String strname = intent.getStringExtra("name");
String strname1 = intent.getStringExtra("name1");
//intent.getExtras();
Log.e("TAG","意图传递的数据="+strname);
Log.e("TAG","意图传递的数据1="+strname1);
// if(savedInstanceState!=null&&!savedInstanceState.isEmpty())
// {
// et1.setText(savedInstanceState.getString("et1")+"恢复之后的");
// et2.setText(savedInstanceState.getString("et2"));
// et3.setText(savedInstanceState.getString("et3"));
// }
} //保存状态
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.e("TAG", "保存应用状态"); outState.putString("et1", et1.getText().toString());
outState.putString("et1",et2.getText().toString());
outState.putString("et1",et3.getText().toString());
} //恢复状态
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.e("TAG", "恢复应用状态"); et1.setText(savedInstanceState.getString("et1")+"恢复之后的");
et2.setText(savedInstanceState.getString("et2"));
et3.setText(savedInstanceState.getString("et3")); } //启动
@Override
protected void onStart() {
super.onStart();
Log.e("TAG","onStart()被调用");
}
//重启
@Override
protected void onRestart() {
super.onRestart();
Log.e("TAG", "onRestart()被调用");
}
//继续
@Override
protected void onResume() {
super.onResume();
Log.e("TAG", "onResume()被调用");
}
//暂停
@Override
protected void onPause() {
super.onPause();
Log.e("TAG", "onPause()被调用");
}
//停止
@Override
protected void onStop() {
super.onStop();
Log.e("TAG", "onStop()被调用");
}
//销毁
@Override
protected void onDestroy() {
super.onDestroy();
Log.e("TAG", "onDestroy()被调用");
}
//点击事件方法
public void bt_OnClick(View v)
{
//显示提示信息
//方法链
Toast.makeText(TextActivity.this, "消息发送成功", Toast.LENGTH_SHORT).show();
}
public void close_OnClick(View v)
{
//关闭应用
finish();
}
}
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword"
android:maxLength="6"/>
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="显式意图"
android:id="@+id/button"
android:layout_weight="1"
android:onClick="login_onClick"/> <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="隐式意图"
android:id="@+id/button3"
android:layout_weight="1"
android:onClick="bt2_onClick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="返回桌面"
android:id="@+id/button4"
android:layout_weight="1"
android:onClick="bt3_onClick"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_gravity="center"
android:textSize="20sp"
android:textColor="@color/colorPrimary"
android:background="@drawable/anniu05"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hanqi.myapplication"> <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission> <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"> <activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TextActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> </application> </manifest>

Android-activity-intent