Android 完成有奖问答页面布局,以及信息接收返回提示以及页面跳转等功能(涉及SharedPreferences、Intent、Toast……多种方法)

按照要求完成如下页面功能:

评分要求:

  1. 完成有奖问答页面布局,要求使用线性布局和相对布局,如何结合不限。
  2. 完成问答题页面布局。
  3. 能够正确使用显式Intent实现页面跳转并传递数据到“你的回答”页面;
  4. 点击“你的回答”页面中的“保存”按钮,能够将答案正确进行简单存储;
  5. 点击保存按钮能够将第四题答案返回“有奖问答”页面,并正确显示,且将“回答”按钮变为“修改”按钮。
  6. 点击“提交按钮”,能够将简单存储中的内容读出;能够正确进行答案判断,并进行处理。

youjiangwenda(activity)

package com.example.myapplication_one;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class youjiangwenda extends AppCompatActivity implements View.OnClickListener{

    EditText editText1,editText2;
    CheckBox checkBox1;
    Button button1,button2;
    TextView textView1;
    String name,name1,name2,check,info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.youjiangwenda);
        init();
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
    }

    private void init() {
        button1 = findViewById(R.id.button1);
        button2 = findViewById(R.id.button2);
        editText1 = findViewById(R.id.editText1);
        editText2 = findViewById(R.id.editText2);
        checkBox1 = findViewById(R.id.checkBox1);
        textView1 = findViewById(R.id.textView4);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button1:
               name1 = editText1.getText().toString();
               name2 = editText2.getText().toString();
               check = checkBox1.getText().toString();

                SharedPreferences.Editor editor =
                        getSharedPreferences("data",MODE_PRIVATE).edit();
                editor.putString("one",name1);
                editor.putString("two",check);
                editor.putString("three",name2);
                editor.commit();
                    Intent intent = new Intent(youjiangwenda.this,youjiangwenda2.class);
                    intent.putExtra("daan1", name1);//键值对
                    intent.putExtra("daan2" ,check);
                    intent.putExtra("daan3" ,name2);
                    startActivityForResult(intent,1001);//1001表示请求码

                break;


            case R.id.button2:
                SharedPreferences preferences =
                        getSharedPreferences("data",MODE_PRIVATE);
                String names1 = preferences.getString("one","");
                String names2 = preferences.getString("two","");
                String names3 = preferences.getString("three","");
                name = names1+names2+names3+info;
                System.out.println(name);
                if(name.equals("1敷衍2p"))
                    Toast.makeText(youjiangwenda.this, "信息完全正确", Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(youjiangwenda.this, "信息出错啦", Toast.LENGTH_LONG).show();

                break;
            default:
                break;
        }



    }

    protected void onActivityResult(int requestCode,int resultCode,Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        switch (requestCode){
            case 1001:
                if(resultCode==1002){
                    info = data.getStringExtra("info");
                    textView1.setText(info);
                    button1.setText("修改");
                    button2.setVisibility(View.VISIBLE);
                }
                break;
        }
    }
}

youjiangwenda.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"
    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_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="有奖问答" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_marginTop="50dp"
        app:srcCompat="@mipmap/kaoshi" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="150dp"
        android:text="答案:" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="307dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="87dp"
        android:layout_marginTop="130dp" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="311dp"
        android:layout_height="36dp"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="180dp"
        app:srcCompat="@mipmap/kaoshi2" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="敷衍"
        android:layout_marginTop="220dp"
        android:layout_marginLeft="70dp"/>

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="敷衔"
        android:layout_marginTop="220dp"
        android:layout_marginLeft="150dp"/>

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="273dp"
        android:layout_height="151dp"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="268dp"
        app:srcCompat="@mipmap/kaoshi3" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="435dp"
        android:text="答案:" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="292dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="87dp"
        android:layout_marginTop="430dp" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="255dp"
        android:layout_height="37dp"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="470dp"
        app:srcCompat="@mipmap/kaoshi4" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="280dp"
        android:layout_marginTop="470dp"
        android:text="回答" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="349dp"
        android:layout_height="57dp"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="535dp"
        android:text="" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"
        android:layout_marginTop="520dp"
        android:layout_centerHorizontal="true"
        android:visibility="invisible"
    />


</RelativeLayout>

注意⚠️:
我们在这个的页面布局中设置了一个提交按钮,但是我们一开始不让其显示,等页面跳转到第二个页面再次返回的时候让其显示。

这就用到了一个属性对:

可见:
android:visibility=“visible”;
Java代码:view.setVisibility(View.VISIBLE);

不可见:
android:visibility=“invisible”;
Java代码:view.setVisibility(View.INVISIBLE);

youjiangwenda2(activity)

package com.example.myapplication_one;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class youjiangwenda2 extends AppCompatActivity {

    TextView textView4,textView2,textView3;
    EditText editText1;
    Button button1;

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

        init();

        Intent intent = getIntent();//getIntent();获取当前intent对象
        String daan1 = intent.getStringExtra("daan1");
        String daan2 = intent.getStringExtra("daan2");
        String daan3 = intent.getStringExtra("daan3");
        textView2.setText("第一题答案:"+daan1);
        textView3.setText("第二题答案:"+daan2);
        textView4.setText("第三题答案:"+daan3);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                //使用Intent对象封装数据
                intent.putExtra("info",editText1.getText().toString());
                //将数据返回到第一个页面
                setResult(1002,intent);
                finish();//关闭当前的Activity(关闭第二个页面,回到第一个页面)
            }
        });

    }



    private void init(){
        textView2 = (TextView) findViewById(R.id.textView2);
        textView3 = (TextView) findViewById(R.id.textView3);
        textView4 = (TextView) findViewById(R.id.textView4);
        button1 = (Button) findViewById(R.id.button1);
        editText1 = (EditText) findViewById(R.id.editText1);
    }

}

youjiangwenda2.xml

<?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_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="你的回答" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="80dp"
        android:text="第一题答案:" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="120dp"
        android:text="第二题答案:" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="160dp"
        android:text="第三题答案:" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="200dp"
        android:text="第四题答案:" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="364dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="250dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="310dp"
        android:text="Button"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

最终功能实现截图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱睡觉的小馨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值