第七次作业(充值功能)
MainActivity
package com.example.phone; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void click(View v) { Intent intent = new Intent(this, SecondActivity.class); String s = ((EditText) findViewById(R.id.editText1)).getText().toString(); if (s.length() == 11 ){ startActivityForResult(intent, 1); } else { Toast.makeText(MainActivity.this, "无效手机号码,请重新输入", 0).show(); } } protected void onActivityResult(int requestCode, int resultCode, Intent intent) { // TODO Auto-generated method stub if(requestCode==1){ switch(resultCode){ case 2: Toast.makeText(this,"充值成功,您的充值金额为"+intent.getIntExtra("money",0)+"元",0).show(); break; case 3: Toast.makeText(this,"充值失败", 0).show(); break; } } } }
SecondActivity
package com.example.phone; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); } public void click(View v) { // TODO Auto-generated method stub String a = ((EditText) findViewById(R.id.editText1)).getText().toString(); int money = Integer.valueOf(a); Intent intent = new Intent(); switch (v.getId()) { // 如果点的按钮是button2 case R.id.button2: // 结果码是2,结果码标记一下是按哪个按钮回去的 intent.putExtra("money", money); setResult(2, intent); finish();// 关闭当前窗口 break; case R.id.button3: setResult(3, intent); finish();// finish点击后当前窗口立即关闭,回到上个窗口的onActivityResult方法 break; } } }
activity_main
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_centerHorizontal="true" android:layout_marginTop="111dp" android:background="#3A5FCD" android:onClick="click" android:text="充值" android:textColor="#FFFFFF" /> <EditText android:id="@+id/editText1" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView1" android:layout_alignParentRight="true" android:phoneNumber="true" android:ems="10" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="102dp" android:text="手机号码" android:textSize="20sp" /> </RelativeLayout>
activity_second
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".SecondActivity" > <EditText android:id="@+id/editText1" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_alignBottom="@+id/textView1" android:layout_alignParentRight="true" android:phoneNumber="true" android:ems="10" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="102dp" android:text="充值金额" android:textSize="20sp" /> <Button android:id="@+id/button2" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/editText1" android:layout_marginLeft="22dp" android:layout_marginTop="98dp" android:background="#3A5FCD" android:textColor="#FFFFFF" android:text="确认充值" android:onClick="click" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/editText1" android:layout_alignRight="@+id/textView1" android:layout_marginBottom="24dp" android:textSize="20sp" /> <Button android:id="@+id/button3" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button2" android:layout_alignBottom="@+id/button2" android:layout_alignRight="@+id/editText1" android:layout_marginRight="25dp" android:background="#3A5FCD" android:textColor="#FFFFFF" android:text="取消充值" android:onClick="click" /> </RelativeLayout>
输入11位数字号码:违规号码
手机号码有效点击充值
输入1000金额点击确认充值
点击取消充值