第七次作业 (手机充值)

痴心易碎 提交于 2019-12-02 07:01:42

作业一:用数据传递和回传实现以下充值功能。

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_above="@id/textview"
        >
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="手机号码"
            android:layout_weight="1"
            />
        <EditText
            android:id="@+id/edittext"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            />
    </LinearLayout>
    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/button"
        />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="充值"
        android:layout_centerInParent="true"
        android:onClick="click"
        />
</RelativeLayout>

package cn.itcase.phonerecharge;

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 MainActivity extends AppCompatActivity {
    private Button button;
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void click(View view) {
        button = (Button) findViewById(R.id.button);
        editText = (EditText) findViewById(R.id.edittext);
        String edittext = editText.getText().toString();
        Intent intent = new Intent(MainActivity.this, secondActivity.class);
        intent.putExtra("sjhm", edittext);
        startActivityForResult(intent, 1);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent czje ) {
        super.onActivityResult(requestCode, resultCode, czje);
        if (requestCode == 1 && resultCode == 2) {
            String acdate = czje.getStringExtra("czje");
            String cz = "您充值了" + acdate + "元";
            Toast.makeText(MainActivity.this, cz, Toast.LENGTH_LONG).show();;
        }
        else  if (requestCode == 1 && resultCode == 4) {
            String accdate = "充值失败";
            Toast.makeText(MainActivity.this, accdate, Toast.LENGTH_LONG).show();
        }
    }

}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="您要为:"
        />
    <TextView
        android:layout_centerInParent="true"
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/textview1"
        />
    <TextView
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/textview2"
        android:text="充值"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@id/textview1"
        android:id="@+id/line"
        >
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="充值金额"
            android:layout_weight="1"
            />
        <EditText
            android:id="@+id/edittext1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            />
    </LinearLayout>
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/line"
        android:text="充值"
        android:onClick="click"
        />
    <TextView
        android:layout_toRightOf="@id/button1"
        android:id="@+id/textview4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="    "
        />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/line"
        android:layout_toRightOf="@id/textview4"
        android:text="取消充值"
        android:onClick="onclick"
        />


</RelativeLayout>

package cn.itcase.phonerecharge;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class secondActivity extends AppCompatActivity {
private TextView textView;
private EditText editText;
private Button button1,button2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondactivity);
        textView=(TextView)findViewById(R.id.textview2);
        Intent intent=getIntent();
        String SJHM=intent.getStringExtra("sjhm");
        textView.setText(SJHM);
    }
    public void click(View view){
        button1=(Button)findViewById(R.id.button1);
        editText=(EditText)findViewById(R.id.edittext1);
        String CZJE=editText.getText().toString();
        Intent intent=new Intent();
        intent.putExtra("czje",CZJE);
        setResult(2,intent);
        finish();
    }
    public  void  onclick(View view){
        button2=(Button)findViewById(R.id.button2);
        Intent intent=new Intent();
                setResult(4,intent);
        finish();
    }
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!