1. 什么是Intent:
Intent的中文意思是目的,主要是用来在不同的Activity中跳转
2. 使用:
新建一个android项目
在java目录下选择新建一个activity,命名为NewActivity,在layout中会自动生成对应的xml文件 ###
3:代码
activity_main.xml
<EditText
android:id="@+id/edit_message"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="@string/message"/>
<Button
android:id="@+id/btn_jump"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="转到新Aty" />
效果图
MainActivity.java
package com.example.administrator.newstudy;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button btnJump;
private TextView edit_message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnJump=(Button) findViewById(R.id.btn_jump);
edit_message=(TextView)findViewById(R.id.edit_message);
btnJump.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this, NewActivity.class);
i.putExtra("text",edit_message.getText().toString());
startActivity(i);
}
});
}
}
activity_new.xml
<TextView
android:id="@+id/TvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/message"/>
NewActivity.java
package com.example.administrator.newstudy;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.w3c.dom.Text;
public class NewActivity extends AppCompatActivity {
String s;
private TextView Tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
Tv=(TextView) findViewById(R.id.TvText);
Tv.setText(getIntent().getStringExtra("text"));
}
}