andriod 中使用意图来开启新窗口并传值
安卓意图分为显示意图和隐式意图
显示意图
Intent intent = new Intent(this,ResultActivity.class);
startActivity(intent);
隐式意图
Intent intent1 = new Intent();
intent1.setAction(Intent.ACTION_CALL);
intent1.setData(Uri.parse("tel:"+119));
startActivity(intent);
主窗体中代码(传值包括开启新意图)
主窗体布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名" />
<RadioGroup
android:id="@+id/rg_gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="男" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="女" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="人妖" />
</RadioGroup>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开始计算"
android:onClick="startCount"/>
</LinearLayout>
主窗体java类
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity
{
private EditText et_name;
private RadioGroup rg_gender;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init()
{
et_name = findViewById(R.id.et_name);
rg_gender = findViewById(R.id.rg_gender);
}
public void startCount(View view)
{
String name = et_name.getText().toString().trim();
int checkedRadioButtonId = rg_gender.getCheckedRadioButtonId();
RadioButton radioButton = findViewById(checkedRadioButtonId);
String gender = radioButton.getText().toString().trim();
Intent intent = new Intent(this,ResultActivity.class);
intent.putExtra("name",name);
intent.putExtra("gender",gender);
startActivity(intent);
}
}
第二窗体中的代码
第二窗体中的布局xml
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="计算结果"
android:textSize="22sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_name"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_gender"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_rp"/>
</LinearLayout>
第二窗体中的java类
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.annotation.Nullable;
public class ResultActivity extends Activity
{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activivy_result);
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String gender = intent.getStringExtra("gender");
TextView tv_name = findViewById(R.id.tv_name);
TextView tv_gender = findViewById(R.id.tv_gender);
tv_name.setText(name);
tv_gender.setText(gender);
}
}