需求:
模拟短信支付功能。创建两个Activity,功能分别为支付首页和短信验证Activity。用户在支付首页点击支付按钮,使用startActivityForResult()方法打开一个新的Activity,在短信验证Activity中模拟输入短信验证码,并判断是否正确,使用setResult()方法将判断的结构回传,最后,在支付首页Activity中提示用户。模拟的短信验证码为“9527”。
运行效果图:
?
?
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity12">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您的消费金额:5000元"
android:textSize="30sp"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginTop="180dp"
android:id="@+id/money"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="短信支付"
android:textSize="26sp"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
android:id="@+id/btn_pay"/>
</RelativeLayout>
?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity13">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="输入短信验证码:"
android:textSize="26sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:id="@+id/textView"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_toRightOf="@+id/textView"
android:layout_alignParentRight="true"
android:id="@+id/et"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认支付"
android:textSize="26sp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView"
android:id="@+id/btn_check"/>
</RelativeLayout>
java代码:
package com.example.myapplication;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity12 extends AppCompatActivity {
private TextView money;
private Button btn_pay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main12);
money = findViewById(R.id.money);
btn_pay = findViewById(R.id.btn_pay);
btn_pay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity12.this,MainActivity13.class);
startActivityForResult(intent,0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 0:
if(resultCode == RESULT_OK){
money.setText(data.getStringExtra("name"));
}
break;
}
}
}
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity13 extends AppCompatActivity {
private EditText et;
private Button btn_check;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main13);
et = findViewById(R.id.et);
btn_check = findViewById(R.id.btn_check);
btn_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = et.getText().toString();
String num = "9527";
Intent intent = new Intent();
if(text.equals("")){
Toast.makeText(MainActivity13.this,"验证码不能为空!",Toast.LENGTH_SHORT).show();
return;
} else if(!text.equals(num)){
intent.putExtra("name","支付失败");
setResult(RESULT_OK,intent);
MainActivity13.this.finish();
} else {
intent.putExtra("name","支付成功");
setResult(RESULT_OK,intent);
MainActivity13.this.finish();
}
}
});
}
}
|