Android studio 实现界面跳转
Android studio 实现界面跳转
标签: 教育
主要大概实现的就是点击主界面的按钮可以跳转到指定界面
实现向10086打电话 发短信以及打开网页打开地图等功能
1.首先在activitiy_main.xml中设计主界面
代码如下:<?xml version="1.0" encoding="utf-8"?>
界面实现结果:
2.接着创建一个activity_second.xml作为跳转界面
界面代码如下:
界面实现结果如下:
3. 接着在MainActivity中编写界面跳转功能
代码如下:
package com.example.internetdemo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.view.View;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view) {
switch (view.getId()){
case R.id.btn_second:
//根据Intent的action启动Activity
Intent intent=new Intent();
intent.setAction("com.example.SECOND_ACTIVITY");
startActivity(intent);
break;
case R.id.btn_call:
//根据Intent的Action启动Activity
callPhone();
intent= new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+"10086"));
startActivity(intent);
break;
case R.id.btn_message:
sendMessage();
break;
case R.id.btn_browser:
openBrowser();
break;
case R.id.btn_map:
openMap();
break;
case R.id.btn_alarm:
creatAlarm();
break;
case R.id.btn_textView:
jumpSecond();
break;
}
}
private void jumpSecond() {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("班级","软件1811");
//2.通过Bundle传递复杂类型:对象、对象集合
Bundle bundle = new Bundle();
bundle.putSerializable("user",new User("张三","13801234567"));
intent.putExtras(bundle);
//3.通过Bunle传递对象集合
ArrayList<User> users=new ArrayList<User>();
users.add(new User("a","13092746578"));
users.add(new User("b","13065986578"));
users.add(new User("c","13092741126"));
bundle.putSerializable("users",users);
intent.putExtras(bundle);
startActivityForResult(intent,101);
}
private void callPhone() {
Intent intent= new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"+"10086"));
//检查运行时权限,若没有则申请,然后在毁掉函数中完成打电话功能
if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CALL_PHONE},
1);
}else{
if(intent.resolveActivity(getPackageManager()) !=null){
startActivity(intent);
}
}
}
//闹钟需要设置权限
private void creatAlarm() {
Intent intent=new Intent(AlarmClock.ACTION_SET_ALARM)
.putExtra(AlarmClock.EXTRA_MESSAGE,“起床”)
.putExtra(AlarmClock.EXTRA_HOUR,6)
.putExtra(AlarmClock.EXTRA_MINUTES,0);
if(intent.resolveActivity(getPackageManager()) != null){
startActivity(intent);
}
}
private void openMap() {
Uri uri=Uri.parse("geo:0?q=1600+Amphitheatre_Parkway,+Mountain+View,+California");
Intent intent=new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
private void openBrowser() {
Intent intent= new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.baidu.com"));
startActivity(intent);
}
private void sendMessage() {
Intent intent = new Intent(Intent.ACTION_SENDTO) ;
intent.setData (Uri.parse ("smsto:10086"));
intent.putExtra("sms_body","10086,你好");
startActivity(intent);
}
}
4.接着在SecondActivity中编写跳转之后的代码
代码如下:
package com.example.internetdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
private TextView tvResult;
private EditText etPhone;
private User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tvResult = findViewById(R.id.btn_textView);
etPhone=findViewById(R.id.et_phone);
Intent intent = getIntent();
String classmate = intent.getStringExtra("班级");
String result = classmate;
Bundle extras = intent.getExtras();
if(extras!=null){
User user=(User) extras.getSerializable("user");
if(user!=null){
result += ","+user.toString();
tvResult.setText(user.getName());
etPhone.setText(user.getPhone());
}
}
//通过按钮的事件监听器
Button btnConfirm=findViewById(R.id.confirm);
btnConfirm.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//获取修改后的手机号,回传给MainActivity
String phone=etPhone.getText().toString();
if(user !=null){
user.setPhone(phone);
Intent intent=new Intent();
Bundle bundle=new Bundle();
bundle.putSerializable("user",user);
setResult(RESULT_OK,intent);
SecondActivity.this.finish();
}
}
});
tvResult.setText(result);
}
}
5.当然我们还需要建一个User的java类
User代码如下:
package com.example.internetdemo;
import java.io.Serializable;
public class User implements Serializable {
private int id;
private String name;
private String phone;
public User(){
}
public User(String name,String phone){
this.name=name;
this.phone=phone;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
6.最后我们就可以在模拟器中运行啦
点击发短信会出现这样的界面
点击打电话会出现这样的界面
这个我就不一 一演示啦 简单举两个例子
这样我们就简单完成一个小功能啦!!
推荐阅读
-
Android软件启动动画及动画结束后跳转的实现方法
-
Android编程实现泡泡聊天界面实例详解(附源码)
-
Android编程之界面跳动提示动画效果实现方法
-
android 跳转到应用通知设置界面的示例
-
jsp跳转页面的方式(jsp实现注册登录界面跳转方法)
-
android studio设计界面变成灰色了该怎么办?
-
Android studio多个项目之间怎么实现快速切换?
-
android游戏载入的activity跳转到游戏主菜单的activity具体实现
-
Android Studio使用ViewPager+Fragment实现滑动菜单Tab效果
-
ViewPager实现带引导小圆点与自动跳转的引导界面