一:Activity概述
activity提供了和用户交互的可视化界面,activity中可以存放很多组件
activity有四种状态:运行态、暂停态、停止态、销毁态。
我们可以通过重写上面提供的方法来完成activity生命周期的操作,但是在一个程序中只能有一个主activity,其他的activity都必须依附于这个主的activity。
当我们要创建activity的视图的时候我们需要通过Intent对象来实现他。
Intent intent = new Intent(MainActivity.this, MyActivity.class);
startActivity(intent);
当我们销毁的时候我们需要在监听器中使用相应的方法。
1.1 Activity的跳转
intent可以跳转到另一个activity中
finish()关闭当前的activity
1.2 Activity的交换数据
intent是信使,但是不具备保存数据的能力,我们需要用bundle将数据绑定到intent
第一个activity
Bundle bundle = new Bundle();
bundle.putCharSequence("name",s1);
intent.putExtras(bundle);
startActivity(intent);
第二个activity
Intent intent1 = getIntent();
Bundle extras = intent1.getExtras();
extras.get("name");
1.3 Activity中文字数据的传递
注意转化
String id = ((EditText) findViewById(R.id.id)).getText().toString();
String name = ((EditText) findViewById(R.id.name)).getText().toString();
String password = ((EditText)findViewById(R.id.password)).getText().toString();
String stuId = ((EditText)findViewById(R.id.stuNo)).getText().toString();
多个activity的转化
package com.example;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import org.w3c.dom.Text;
public class LoginActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String id = bundle.getString("id");
String name = bundle.getString("name");
String stuId = bundle.getString("stuId");
TextView t1 = findViewById(R.id.t1);
TextView t2 = findViewById(R.id.t2);
TextView t3 = findViewById(R.id.t3);
t1.setText(id);
t2.setText(name);
t3.setText(stuId);
}
}
package com.example;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = ((EditText) findViewById(R.id.id)).getText().toString();
String name = ((EditText) findViewById(R.id.name)).getText().toString();
String password = ((EditText)findViewById(R.id.password)).getText().toString();
String stuId = ((EditText)findViewById(R.id.stuNo)).getText().toString();
if ("".equals(id) || "".equals(name) || "".equals(password) || "".equals(stuId)) {
Toast.makeText(MainActivity.this, "请将信息输入 完整", Toast.LENGTH_SHORT).show();
} else if (!"".equals(id) && !"".equals(name) && !"".equals(password) && !"".equals(stuId)) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
Bundle bundle = new Bundle();
bundle.putCharSequence("id", id);
bundle.putCharSequence("name", name);
bundle.putCharSequence("password", password);
bundle.putCharSequence("stuId", stuId);
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
1.4 调用另一个Activiy返回结果(startActivityForResult)
待补充
二:Fragment
2.1 加载Fragment
创建一个布局文件
将该布局文件加载到Fragment中
package com.example;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class ListFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//加载布局文件
View view = inflater.inflate(R.layout.ftagment,container,false);
return view;
}
}
将Fragment添加到activity中
直接在布局文件中添加Fragment,在activity_main中添加fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.example.ListFragment"
android:id="@+id/frg"
/>
</LinearLayout>
动态添加Fragment,当activity正在运行的时候添加fragment
ListFragment listFragment = new ListFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(android.R.id.content,listFragment);
fragmentTransaction.commit();
2.2 Fragment实例
package com.example;
import android.view.View;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView1 = findViewById(R.id.id1);
ImageView imageView2 = findViewById(R.id.id2);
imageView1.setOnClickListener(onClickListener);
imageView2.setOnClickListener(onClickListener);
}
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
Fragment fragment = null;
switch (v.getId()){
case R.id.id1:
fragment = new WeChatFragment();
break;
case R.id.id2:
fragment = new FindFragment();
break;
default:
break;
}
ft.replace(R.id.f1,fragment);
ft.commit();
}
};
}
三:Intent
3.1 Intent对象属性
3.1.1 setComponent
我们可以通过setCompotent来启动另一个activity
Intent intent = new Intent();
ComponentName componentName = new ComponentName("com.example","com.example.LoginActivity");
intent.setComponent(componentName);
startActivity(intent);
3.1.2 Action和Data
打电话和发短信的权限设置
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
package com.example;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView1 = findViewById(R.id.i1);
ImageView imageView2 = findViewById(R.id.i2);
imageView1.setOnClickListener(l);
imageView2.setOnClickListener(l);
}
View.OnClickListener l = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
ImageButton imageButton = (ImageButton) v;
switch (imageButton.getId()){
case R.id.i1:
//拨打电话
intent.setAction(intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:18306857492"));
startActivity(intent);
break;
case R.id.i2:
intent.setAction(intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:5555555"));
intent.putExtra("sms_body","Welcome");
startActivity(intent);
break;
}
}
};
}
3.1.3 Action和Categories
具体内容查看API
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
//返回桌面
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
3.1.4 Extras和Flags
多个activity交换数据
putExtras()
getExtras()
intent.setFlags(intent.FLAG_ACTIVITY_NO_HISTORY)
这句话意思是一旦离开了界面我们就需要从历史栈中重新加载activity
3.2 Intent种类
显示intent
Intent intent = new Intent(MainActivity.this,AppCompatActivity.class);
隐示Intent
我们直接创建action、data、category来使用Intent
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
//返回桌面
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);