转载请注明出处:Intent 实现活动之间的跳转_intent1 = intent(this, registeredactivity::_Mr_Leixiansheng的博客-CSDN博客
步骤:
1、新建要跳转的活动和布局文件
2、在AndroidManifest中实现注册
3、要跳转的活动中实现跳转
新建的活动和布局
package com.example.administrator.intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
/**
* Created by Administrator on 2016/12/10.
*/
public class IntentActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent_layout);
}
}
<?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">
<TextView
android:text="这是跳转后的活动"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
主程序和布局
/*
活动之间的跳转Intent
*/
package com.example.administrator.intent;
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, IntentActivity.class); //跳转设置
startActivity(intent); //开始跳转
}
});
}
}
<?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">
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button" />
</LinearLayout>
Kotlin写法:
package com.leixiansheng.kotlintest
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
startActivity(Intent(this, IntentActivity::class.java))
}
}
}
隐式启动:
在manifest 定义 “com.leixiansheng.START” 作为启动标识(自定义)
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.leixiansheng.START"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
然后在代码 intent 跳转时加入标识
button.setOnClickListener(v -> {
Intent intent = new Intent("com.leixiansheng.START");
startActivity(intent);
});
Intent 常用方法记录:
1、打开浏览器,跳转网页
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
2、拨打电话(注意权限)
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
3、直接拨号(注意权限)
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);