布局文件 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
<android:id="button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button"/>
</LinearLayout>
新建布局文件activity_intent.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IntentActivity">
<Button
<android:id="button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button2"/>
</LinearLayout>
IntentActivity.java
package com.example.toast;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class IntentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent);
Button button=(Button)findViewById(R.id.button2);
button.setOnClickListener(new View OnclickListener(){
@Override
public void onClick(View v){
Intent intent=new Intent(IntentActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
主文件 MainActivity.java
package com.example.toast;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.content.Intent;
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.button1);
button.setOnclickListener(new View.OnclickListener(){
@Override
public void onClick(View v){
Toast.makeText(MainActivity.this,"hello wqsy",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(MainActivity.this,IntentActivity.class);
startActivity(intent);
finish();
}
});
}
}
finish();加上是销毁程序
Intent转换第一页面到第二页面 返回第一页面