第七次作业
所需截图:
所需代码:
package com.example.myapplication;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText et_number;
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_number = findViewById(R.id.et_number);
tv_show = findViewById(R.id.tv_show);
}
public void one(View view) {
Intent intent = new Intent();
intent.setClass(this, Main2Activity.class);
intent.putExtra("number", et_number.getText().toString().trim());
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == 2) {
String show = data.getStringExtra("data");
tv_show.setTextColor(Color.RED);
tv_show.setText("充值成功充值了"+show+"元");
}
if (requestCode == 1 && resultCode == 3) {
tv_show.setTextColor(Color.RED);
tv_show.setText("充值失败");
}
}
}
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class Main2Activity extends AppCompatActivity {
private TextView tv_number;
private EditText et_money;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv_number = findViewById(R.id.tv_number);
et_money = findViewById(R.id.et_money);
Intent intent = getIntent();
tv_number.setText(intent.getStringExtra("number"));
}
public void two1(View view) {
Intent intent = new Intent();
intent.putExtra("data", et_money.getText().toString().trim());
setResult(2, intent);
finish();
}
public void two2(View view) {
Intent intent = new Intent();
setResult(3, intent);
finish();
}
}
?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="60dp"
android:layout_marginRight="20dp"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机号码:"
android:textColor="@android:color/black"
android:textSize="30sp" />
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center" />
</LinearLayout>
<Button
android:layout_width="150dp"
android:layout_height="60dp"
android:layout_centerInParent="true"
android:onClick="one"
android:text="充值"
android:textSize="35sp" />
<TextView
android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="15dp"
android:layout_marginBottom="200dp"
android:textColor="@android:color/black"
android:textSize="28dp" />
</RelativeLayout>
<?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=".Main2Activity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:layout_marginRight="30dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您的号码是:"
android:textSize="35sp" />
<TextView
android:id="@+id/tv_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="50dp"
android:layout_marginRight="40dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="充值金额:"
android:textSize="35sp" />
<EditText
android:id="@+id/et_money"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:orientation="horizontal">
<Button
android:layout_width="120dp"
android:layout_height="60dp"
android:layout_marginLeft="35dp"
android:onClick="two1"
android:text="充值"
android:textSize="32sp" />
<Button
android:layout_width="180dp"
android:layout_height="60dp"
android:layout_marginLeft="35dp"
android:onClick="two2"
android:text="取消充值"
android:textSize="32sp" />
</LinearLayout>
</LinearLayout>