Android Studio使用Kotlin语言创建一个心理测试APP

Android Studio使用Kotlin语言创建一个心理测试APP

菜单功能

拖延指数测验、急迫性指数测验、关于、退出

文件配置

  1. activity_main.xml
<LinearLayout 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:orientation="horizontal"
    tools:context=".MainActivity">
    <fragment
        android:id="@+id/leftFrag"
        android:name="com.example.one_e.LeftFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout="@layout/fragment_left" />
    <FrameLayout
        android:id="@+id/rightFrag"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3">
    </FrameLayout>
</LinearLayout>
  1. 放置菜单的Fragment配置文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#eeaadd"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LeftFragment">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/titleContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="27sp"
            android:text="@string/title_app"
            android:layout_gravity="center_horizontal"
            />
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/title_psychology_test1"
            />
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/title_psychology_test2"
            />
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/title_psychology_about"
            />
        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/title_exit"
            />
    </LinearLayout>
</FrameLayout>
  1. 拖延指数测验功能的Fragment配置文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#aaffff"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RightFragment">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/itemContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:layout_marginTop="100dp"
            android:layout_gravity="center_horizontal"
            android:text="@string/title_app"/>
        <RadioGroup
            android:id="@+id/rgResult1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal">
            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/psychology_test1_answer_a" />
            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/psychology_test1_answer_b" />
            <RadioButton
                android:id="@+id/radio3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/psychology_test1_answer_c" />
            <RadioButton
                android:id="@+id/radio4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/psychology_test1_answer_d" />
        </RadioGroup>
    </LinearLayout>
</FrameLayout>
  1. 急迫性指数测验功能的Fragment配置文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#aaffff"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TestFragment">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/testContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"
            android:layout_marginTop="100dp"
            android:layout_gravity="center_horizontal"
            android:text="@string/title_app"/>
        <RadioGroup
            android:id="@+id/rgResult2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal">
            <RadioButton
                android:id="@+id/radio5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/psychology_test2_answer_a" />
            <RadioButton
                android:id="@+id/radio6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/psychology_test2_answer_b" />
            <RadioButton
                android:id="@+id/radio7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/psychology_test2_answer_c" />
        </RadioGroup>
    </LinearLayout>
</FrameLayout>
  1. 关于的Fragment配置文件]
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#aaffff"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AboutFragment">
    <TextView
        android:id="@+id/aboutContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:text="@string/title_help"/>
</FrameLayout>
  1. string.xml:
<resources>
    <string name="app_name">心理测试应用实例</string>`
    <string name="title_app">心理测试</string>
    <string name="title_psychology_test1">拖延指数测验</string>
    <string name="title_psychology_test2">急迫性指数测验</string>
    <string name="title_psychology_about">关于APP</string>
    <string name="title_exit">退出</string>
    <string name="title_psychology_result">测试的结果</string>
    <string-array name="psychology_test1">
        <item>为了避免对棘手的难题采取行动,我于是寻找理由和借口。</item>
        <item>为使困难的工作能被执行,对执行者下压力是必要的。</item>
        <item>我经常采取折衷办法以避免或延缓不愉快的事是困难的工作。</item>
        <item>我遭遇了太多足以妨碍完成重大任务的干扰与危机。</item>
        <item>当被迫从事一项不愉快的决策时,我避免直截了当的答复。</item>
        <item>我对重要的行动计划的追踪工作一般不予理会。</item>
        <item>试图令他人为管理者执行不愉快的工作。</item>
        <item>我经常将重要工作安排在下午处理,或者携回家里,以便在夜晚或周末处理它。</item>
        <item>我在过分疲劳(或过分紧张、或过分泄气、或太受抑制)时,以致无法处理所面对的困难任务。</item>
        <item>在着手处理一件艰难的任务之前,我喜欢清除桌上的每一个物件。</item>
    </string-array>
    <string name="psychology_test1_answer_a">A 非常同意</string>
    <string name="psychology_test1_answer_b">B 略表同意</string>
    <string name="psychology_test1_answer_c">C 略表不同意</string>
    <string name="psychology_test1_answer_d">D 极不同意</string>
    <string name="psychology_test1_result_1">你不是拖延者,偶尔有拖延习惯!</string>
    <string name="psychology_test1_result_2">你有拖延的毛病,但不太严重!</string>
    <string name="psychology_test1_result_3">或许你有很严重的拖延习惯!</string>
    <string-array name="psychology_test2">
        <item>我在压力之下表现最好。</item>
        <item>我常归咎外在环境太匆忙或紧张,以致无法作深入的自我反省。</item>
        <item>我常因周围的人或事动作太慢而不耐烦。我讨厌等待或排队。</item>
        <item>我休息时会觉得不安。</item>
        <item>我似乎永远在赶时间。</item>
        <item>我常为了完成某项事情而拒人于千里之外。</item>
        <item>我只要片刻没和办公室联系就觉得不安。</item>
        <item>我在做一件事时常会想到另一件事。</item>
        <item>我处理危机时表现最好。</item>
        <item>处理突发状况的兴奋感,似乎比慢工出细活更让我觉得有成就。</item>
        <item>我常为了处理突发状况,牺牲和亲友共处时间。</item>
        <item>当我为了处理突发状况,必须取消约会或中途离开,我认为别人应该能谅解。</item>
        <item>我觉得处理突发状况让一天的生活更有意义。</item>
        <item>我常边工作边吃饭。</item>
        <item>我一直认为总有一天能做我真正想做的事情。</item>
        <item>一天下来办公桌上“已办”文件如果堆得高高的,我会很有成就感。</item>
    </string-array>
    <string name="psychology_test2_answer_a">A 从不</string>
    <string name="psychology_test2_answer_b">B 有时候</string>
    <string name="psychology_test2_answer_c">C 常常</string>
    <string name="psychology_test2_result_1">低度急迫性心态</string>
    <string name="psychology_test2_result_2">强烈急迫性心态</string>
    <string name="psychology_test2_result_3">已经到了严重急迫性的程度</string>
    <!-- TODO: Remove or change this placeholder text -->
    <string name="title_help">心理测试APP\n作者:\n南昌大学信息工程学院\n\n
        素材来源:https://wenku.baidu.com/view/3881170fbf23482fb4daa58da0116c175f0e1efe.html\n\n
        时间:二零二零年六月三十日
    </string>
</resources>

代码

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        button1.setOnClickListener {
            val fragment = RightFragment()
            val transaction:FragmentTransaction = supportFragmentManager.beginTransaction()
            transaction.replace(R.id.rightFrag,fragment)
            transaction.commit()
        }
        button2.setOnClickListener {
            val fragment = TestFragment()
            val transaction:FragmentTransaction = supportFragmentManager.beginTransaction()
            transaction.replace(R.id.rightFrag,fragment)
            transaction.commit()
        }
        button3.setOnClickListener {
            val fragment = AboutFragment()
            val transaction:FragmentTransaction = supportFragmentManager.beginTransaction()
            transaction.replace(R.id.rightFrag,fragment)
            transaction.commit()
        }
        button4.setOnClickListener {
            finishAffinity()
        }
    }
}
class LeftFragment : Fragment() {
    override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_left,container,false)
    }
}
class RightFragment : Fragment() {
    private var result:Int = 0
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_right,container,false)
    }
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val res: Resources = resources
        val itemList = res.getStringArray(R.array.psychology_test1)
        val r1 = res.getString(R.string.psychology_test1_result_1)
        val r2 = res.getString(R.string.psychology_test1_result_2)
        val r3 = res.getString(R.string.psychology_test1_result_3)
        var i:Int = 1
        updateCon(itemList, i)
        rgResult1.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.radio1 -> {
                    result += 1
                    ++i
                    if(i<=10) {
                        updateCon(itemList, i)
                        rgResult1.clearCheck()
                    }
                    else{
                        rgResult1.visibility = View.INVISIBLE
                        when (result) {
                            in 10..19 -> {
                                itemContent.text=r1
                            }
                            in 20..29 -> {
                                itemContent.text=r2
                            }
                            else -> {
                                itemContent.text=r3
                            }
                        }
                    }
                }
                R.id.radio2 -> {
                    result += 2
                    ++i
                    if(i<=10){
                        updateCon(itemList, i)
                        rgResult1.clearCheck()
                    }
                    else{
                        rgResult1.visibility = View.INVISIBLE
                        when (result) {
                            in 10..19 -> {
                                itemContent.text=r1
                            }
                            in 20..29 -> {
                                itemContent.text=r2
                            }
                            else -> {
                                itemContent.text=r3
                            }
                        }
                    }
                }
                R.id.radio3 -> {
                    result += 3
                    ++i
                    if(i<=10){
                        updateCon(itemList, i)
                        rgResult1.clearCheck()
                    }
                    else{
                        rgResult1.visibility = View.INVISIBLE
                        when (result) {
                            in 10..19 -> {
                                itemContent.text=r1
                            }
                            in 20..29 -> {
                                itemContent.text=r2
                            }
                            else -> {
                                itemContent.text=r3
                            }
                        }
                    }
                }
                R.id.radio4 -> {
                    result += 4
                    ++i
                    if(i<=10){
                        updateCon(itemList, i)
                        rgResult1.clearCheck()
                    }
                    else{
                        rgResult1.visibility = View.INVISIBLE
                        when (result) {
                            in 10..19 -> {
                                itemContent.text=r1
                            }
                            in 20..29 -> {
                                itemContent.text=r2
                            }
                            else -> {
                                itemContent.text=r3
                            }
                        }
                    }
                }
            }
        }
    }
    private fun updateCon(items:Array<String>,index:Int){
        itemContent.text = index.toString() + "." + items[index-1]
    }
}

上面是拖延指数测验的代码,另一个测试代码与之类似,关于的代码与菜单的代码一致。

运行结果

结果之一

总结

文字描述比较少,有疑问可在评论中提出。
  • 6
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值