之前做三下乡时候涉及到垃圾分类这个话题,于是我就用AS做了一个垃圾分类知识竞赛小程序。
迎面而来的是一个丑陋的欢迎界面
XML布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="欢迎来到垃圾分类知识竞赛!"
android:textSize="20dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="325dp"
android:src="@drawable/welcome"/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="答题规则"
android:textSize="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="20dp"
android:gravity="center"
android:text="判断下列垃圾是属于哪一种垃圾,选择正确的选项,帮助它们找到回家的路吧!"
android:textSize="20dp" />
<Button
android:id="@+id/start_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#FF0000"
android:text="开始答题"/>
</LinearLayout>
这是第一个问题活动界面的java代码
package com.example.garbage_classification;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
public class First_question extends AppCompatActivity implements View.OnClickListener {
private static final String Acon = "你选择了可回收垃圾";
private static final String Bcon = "你选择了厨余垃圾";
private static final String Ccon = "你选择了有害垃圾";
private static final String Dcon = "你选择了其他垃圾";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_question);
findViewById(R.id.recyclable).setOnClickListener(this);
findViewById(R.id.kitchen).setOnClickListener(this);
findViewById(R.id.harm).setOnClickListener(this);
findViewById(R.id.other).setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(this,First_answer.class);
Bundle bundle = new Bundle();
int id = view.getId();
if (id == R.id.recyclable) {
bundle.putInt("code",1);
bundle.putString("answer",Acon);
} else if (id == R.id.kitchen){
bundle.putInt("code",0);
bundle.putString("answer",Bcon);
} else if (id == R.id.harm) {
bundle.putInt("code",0);
bundle.putString("answer",Ccon);
} else if (id == R.id.other) {
bundle.putInt("code",0);
bundle.putString("answer",Dcon);
}
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
Log.d("wangyu", "onClick: 可以执行到这里");
startActivity(intent);
}
}
这是问题1的布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="请看第一道问题:塑料瓶是什么类型的垃圾?"
android:textSize="18sp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:src="@drawable/plastic_bottle"/>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="2">
<ImageButton
android:id="@+id/recyclable"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/recyclable"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:scaleType="fitXY"/>
<ImageButton
android:id="@+id/kitchen"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/kitchen_waste"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:scaleType="fitXY"/>
<ImageButton
android:id="@+id/harm"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/harm"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:scaleType="fitXY"/>
<ImageButton
android:id="@+id/other"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="@drawable/other"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:scaleType="fitXY"/>
</GridLayout>
</LinearLayout>
布局效果是这样的,太丑了对吧,哈哈。
然后这是第一个回答的java代码:
package com.example.garbage_classification;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class First_answer extends AppCompatActivity implements View.OnClickListener{
private static final String right = "恭喜你答对了。";
private static final String wrong = "很遗憾,你回答错误。";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_answer);
findViewById(R.id.go_on1_btn).setOnClickListener(this);
//问题出在调用 findViewById(R.id.go_on1_btn).setOnClickListener(this) 的位置。
// 在 onCreate 方法中,你应该先通过 setContentView(R.layout.activity_first_answer) 设置布局文件,
// 然后才能使用 findViewById 方法来找到布局文件中的控件。因此,你需要将这两行代码的位置进行调换:
ImageView imageView = findViewById(R.id.result);
TextView tv = findViewById(R.id.explain);
Bundle bundle = getIntent().getExtras();
String result_context = bundle.getString("answer");
int result_code = bundle.getInt("code");
String explain = "";
if(result_code==1){
explain = result_context+","+right+"矿泉水瓶属于可回收垃圾,这是因为矿泉水瓶是聚苯乙烯材料制作的,这种材料回收后打碎成颗粒仍然可以再利用,并且是一种很好的材料,能够产生很大的利用价值。";
tv.setText(explain);
@SuppressLint("UseCompatLoadingForDrawables") Drawable drawable = getResources().getDrawable(R.drawable.victory);
imageView.setImageDrawable(drawable);
}else {
explain = result_context+","+wrong+"矿泉水瓶属于可回收垃圾,这是因为矿泉水瓶是聚苯乙烯材料制作的,这种材料回收后打碎成颗粒仍然可以再利用,并且是一种很好的材料,能够产生很大的利用价值。";
tv.setText(explain);
@SuppressLint("UseCompatLoadingForDrawables") Drawable drawable = getResources().getDrawable(R.drawable.defeat);
imageView.setImageDrawable(drawable);
}
}
@Override
public void onClick(View view) {
Intent intent = new Intent(this, Second_question.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
答对没有奖励:
答错有惩罚哦(悲:
运行个虚拟机给我电脑内存搞上90%了。
第一个回答的布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="20dp"
android:text="正确答案揭晓!"
android:textSize="20dp" />
<ImageView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="300dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:text="答案解析"
android:textSize="20dp" />
<TextView
android:id="@+id/explain"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_margin="20dp"
android:gravity="center"
android:text=""
android:textSize="20dp" />
<Button
android:id="@+id/go_on1_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#FF0000"
android:text="继续挑战"/>
</LinearLayout>
接下来的问题及其回答的代码和布局XML文件就是和第一个类似啦。额,虽然很丑。
最后是MVP结算(喜!
最后还调用了Android的系统Action,可以直接给我打电话催更,存粹娱乐的低成本制作,仅供消遣!!!