按照要求完成如下页面功能:
![](https://img-blog.csdnimg.cn/397e367902ed4e029f654a0d96426b92.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5oyg5aS05bCP6Lev6aOe,size_35,color_FFFFFF,t_70,g_se,x_16)
评分要求:
- 完成有奖问答页面布局,要求使用线性布局和相对布局,如何结合不限。
- 完成问答题页面布局。
- 能够正确使用显式Intent实现页面跳转并传递数据到“你的回答”页面;
- 点击“你的回答”页面中的“保存”按钮,能够将答案正确进行简单存储;
- 点击保存按钮能够将第四题答案返回“有奖问答”页面,并正确显示,且将“回答”按钮变为“修改”按钮。
- 点击“提交按钮”,能够将简单存储中的内容读出;能够正确进行答案判断,并进行处理。
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>
![](https://img-blog.csdnimg.cn/09d5cb97ece4473092267018d3422eaa.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5oyg5aS05bCP6Lev6aOe,size_40,color_FFFFFF,t_70,g_se,x_16)
注意⚠️:
我们在这个的页面布局中设置了一个提交按钮,但是我们一开始不让其显示,等页面跳转到第二个页面再次返回的时候让其显示。
这就用到了一个属性对:
可见:
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>
![](https://img-blog.csdnimg.cn/0934fe588e7c462ca81943a623e05f36.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5oyg5aS05bCP6Lev6aOe,size_40,color_FFFFFF,t_70,g_se,x_16)
最终功能实现截图:
![](https://img-blog.csdnimg.cn/361374737f914ff9af15abc24d0f1de5.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5oyg5aS05bCP6Lev6aOe,size_17,color_FFFFFF,t_70,g_se,x_16)
![](https://img-blog.csdnimg.cn/75b6f8088b2e48aba7fc9d127ac2c5f5.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5oyg5aS05bCP6Lev6aOe,size_17,color_FFFFFF,t_70,g_se,x_16)
![](https://img-blog.csdnimg.cn/585b0675b4b24a67814012a43f0a6a84.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5oyg5aS05bCP6Lev6aOe,size_17,color_FFFFFF,t_70,g_se,x_16)
![](https://img-blog.csdnimg.cn/eb861dcc49fb42d2a95bfbaa11756f6a.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5oyg5aS05bCP6Lev6aOe,size_16,color_FFFFFF,t_70,g_se,x_16)