如何在Android中使用Intent实现一个页面跳转功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
首先需要在MainActivity中对页面注册,比如
新建被跳转的页面OtherActivity,其对应的xml文件如下
activity_other
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个Activity"/>
</LinearLayout>
Java代码
OtherActivity
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class OtherActivity extends AppCompatActivity {
@Override
public void setContentView(View view) {
super.setContentView(R.layout.activity_other);
}
}
程序主界面activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个Activity"/>
<Button
android:id="@+id/start_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="页面跳转"/>
</LinearLayout>
Java代码
MainActivity
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button startButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = findViewById(R.id.start_btn);
startButton.setOnClickListener(new ButtonListener());
}
class ButtonListener implements View.OnClickListener{
@Override
public void onClick(View v) {
//当点击事件触发后执行,启动OtherActivity
//创建一个Intent对象
Intent intent =new Intent();
intent.setClass(MainActivity.this,OtherActivity.class);//从MainActivity跳转到OtherActivity
startActivity(intent);
}
}
}
另外除了上述的显式Intent,还有隐式Intent,隐式Intent可以用来传递数组及动作状态
比如在MainActivity中
//当点击事件触发后执行,启动OtherActivity
//创建一个Intent对象
Intent intent =new Intent();
intent.setClass(MainActivity.this,OtherActivity.class);//从MainActivity跳转到OtherActivity
intent.putExtra("姓名","小李");
startActivity(intent);
在被跳转的OtherActivity中
Intent intent =new Intent();
String name = intent.getStringExtra("姓名");
可以接收由MainActivity传来的数据
又或者
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
可以调用拨打电话界面并设定预设号码为10086
还可以设置网址的跳转,显示地理位置等
如设置为跳转打开网址时,需要在AndroidManifast中注册一下<data android:scheme="http"/>
如下:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="http"/>
</intent-filter>
</activity>
看完上述内容,你们掌握如何在Android中使用Intent实现一个页面跳转功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。