300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Android(Kotlin)图片按钮 单选框 复选框(实验课)

Android(Kotlin)图片按钮 单选框 复选框(实验课)

时间:2019-09-09 22:33:51

相关推荐

Android(Kotlin)图片按钮 单选框 复选框(实验课)

Android的一次“实验课布置的内容”

里面有一些可以擦考的东西

所以借着CSDN来记个笔记

其实也没啥 第一次写 留个纪念

图片按钮,单选框,复选框(话不多说上代码)

button_layout.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><ImageButtonandroid:layout_width="100dp"android:layout_height="100dp"android:id="@+id/iButton"android:background="@drawable/bg"/><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/chinese"android:text="Chinese"/><CheckBoxandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/english"android:text="English"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/cButton"android:text="复选按钮确定"/><RadioGroupandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"

ButtonActivity.kt

package com.example.ui3import android.content.DialogInterfaceimport androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport androidx.appcompat.app.AlertDialogimport androidx.fragment.app.DialogFragmentimport kotlinx.android.synthetic.main.button_layout.*class ButtondoActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.button_layout)iButton.setOnClickListener{var iTip:String = "图片"setTips(iTip)}cButton.setOnClickListener{var cTip:String = "复选"setTips(cTip)}rButton.setOnClickListener{val rTip:String = "单选"setTips(rTip)}}//根据按钮,设置输出内容fun setTips(tipString: String){var tips:String = getString(R.string.tips,tipString)tipSomething(tips)}//创建并显示对话框fun tipSomething(tipEnd: String){val builder = AlertDialog.Builder(this)builder.setMessage(tipEnd).setPositiveButton("确认",DialogInterface.OnClickListener { dialog, id ->// FIRE ZE MISSILES!})// Create the AlertDialog object and return itbuilder.create()builder.show()}}

string.xml

<resources><string name="app_name">UI3</string><string name="tips">您点击了%1$s按钮!</string></resources>

这里插一段废话,string.xml写这些东西是为了简化代码,

实验报告的内容是根据点击不同按钮弹出内容不同的对话框,

因此在Activity读取string.xml内容(不提动态,磨磨唧唧的)

在占位符”%1$s”填写相应内动,来修改对话框所提示的内容,

注释还算详细。。。。。。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="/apk/res/android"package="com.example.ui3"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.UI3"><activity android:name=".ButtondoActivity"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></intent-filter></activity></application></manifest>

图片按钮的图片就不上传了,,,目录结构也都明白

效果图如下:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。