第八次作业
1.8
1.1
<?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=".MainActivity">
<?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=".MainActivity">
<TextView
android:id="@+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="120dp"
android:text="手机号码" />
android:id="@+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="120dp"
android:text="手机号码" />
<EditText
android:id="@+id/tb_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/text_1"
android:layout_alignBottom="@id/text_1"
android:layout_alignParentEnd="true"
android:layout_marginLeft="-11dp"
android:layout_marginEnd="3dp"
android:layout_marginBottom="-69dp"
android:layout_toRightOf="@id/text_1"
android:hint="" />
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="360dp"
android:text="充值"
android:onClick="click"/>
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="360dp"
android:text="充值"
android:onClick="click"/>
<TextView
android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:textSize="28dp"/>
</RelativeLayout>
1.2
<?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=".Second">
<TextView
android:id="@+id/text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="180dp"
android:text="手机号码为" />
<TextView
android:id="@+id/text_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/text_2"
android:layout_alignBottom="@id/text_2"
android:text=" "/>
android:id="@+id/text_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/text_2"
android:layout_alignBottom="@id/text_2"
android:text=" "/>
<TextView
android:id="@+id/text_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="240dp"
android:text="充值金额"/>
android:id="@+id/text_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="240dp"
android:text="充值金额"/>
<EditText
android:id="@+id/tb_2"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/text_5"
android:layout_centerHorizontal="true"/>
android:id="@+id/tb_2"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/text_5"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/bt_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/text_5"
android:layout_alignParentBottom="true"
android:layout_marginLeft="32dp"
android:layout_marginBottom="180dp"
android:layout_toRightOf="@id/tb_2"
android:text="充值"
android:onClick="one"/>
android:id="@+id/bt_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/text_5"
android:layout_alignParentBottom="true"
android:layout_marginLeft="32dp"
android:layout_marginBottom="180dp"
android:layout_toRightOf="@id/tb_2"
android:text="充值"
android:onClick="one"/>
<Button
android:id="@+id/bt_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="180dp"
android:layout_toLeftOf="@id/bt_2"
android:layout_toRightOf="@id/tb_2"
android:text="取消充值"
android:onClick="two"/>
</RelativeLayout>
android:id="@+id/bt_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="180dp"
android:layout_toLeftOf="@id/bt_2"
android:layout_toRightOf="@id/tb_2"
android:text="取消充值"
android:onClick="two"/>
</RelativeLayout>
1.3
package com.example.couzhi;
package com.example.couzhi;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
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 android.widget.Toast;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
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 android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText tb_1;
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb_1=findViewById(R.id.tb_1);
tv_show =findViewById(R.id.tv_show);
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb_1=findViewById(R.id.tb_1);
tv_show =findViewById(R.id.tv_show);
}
//点按钮开启第二个窗口
public void click(View V){
Intent intent=new Intent();
intent.setClass(this,Second.class);
intent.putExtra("number",tb_1.getText().toString().trim());
startActivityForResult(intent,1);
//点按钮开启第二个窗口
public void click(View V){
Intent intent=new Intent();
intent.setClass(this,Second.class);
intent.putExtra("number",tb_1.getText().toString().trim());
startActivityForResult(intent,1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if(requestCode==1&&resultCode==2){
String show=intent.getStringExtra("data");
String num=tb_1.getText().toString().trim();
SpannableStringBuilder builder=new SpannableStringBuilder("你为号码"+num+"\n 充值了"+show+"钱");
ForegroundColorSpan span1=new ForegroundColorSpan(Color.RED);
builder.setSpan(span1,4,4+num.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv_show.setText(builder);
}
String show=intent.getStringExtra("data");
String num=tb_1.getText().toString().trim();
SpannableStringBuilder builder=new SpannableStringBuilder("你为号码"+num+"\n 充值了"+show+"钱");
ForegroundColorSpan span1=new ForegroundColorSpan(Color.RED);
builder.setSpan(span1,4,4+num.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv_show.setText(builder);
}
if(requestCode==1&&resultCode==3){
tv_show.setTextColor(Color.RED);
tv_show.setText("充值失败");
}
}
}
tv_show.setTextColor(Color.RED);
tv_show.setText("充值失败");
}
}
}
1.4
package com.example.couzhi;
package com.example.couzhi;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class Second extends AppCompatActivity {
//第二个页面的电话号码及充值金额
private TextView text_3;
private EditText tb_2;
private TextView text_3;
private EditText tb_2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
text_3=findViewById(R.id.text_3);
tb_2=findViewById(R.id.tb_2);
Intent intent=getIntent();
text_3.setText(intent.getStringExtra("number"));
tb_2.setText(intent.getStringExtra("data"));
}
public void one(View v){
Intent intent=new Intent();
intent.putExtra("data",tb_2.getText().toString().trim());
setResult(2,intent);
finish();
}
public void two(View v){
Intent intent=new Intent();
setResult(3,intent);
finish();
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
text_3=findViewById(R.id.text_3);
tb_2=findViewById(R.id.tb_2);
Intent intent=getIntent();
text_3.setText(intent.getStringExtra("number"));
tb_2.setText(intent.getStringExtra("data"));
}
public void one(View v){
Intent intent=new Intent();
intent.putExtra("data",tb_2.getText().toString().trim());
setResult(2,intent);
finish();
}
public void two(View v){
Intent intent=new Intent();
setResult(3,intent);
finish();
}
}