1.返回键实现对话框弹出是否退出应用程序

package com.example.zuoye1;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} @Override
public void onBackPressed(){
//声明对象
AlertDialog dialog;
AlertDialog.Builder builder=new AlertDialog.Builder(this)
.setTitle("普通对话框")//设置对话框的标题
.setIcon(R.mipmap.ic_launcher)//设置设置标题图标
.setMessage("是否确定退出应用:")//设置对话框的提示信息
//添加‘确定’按钮
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();//关闭对话框
MainActivity.this.finish();//关闭MainActivity
}
})//添加“取消”按钮
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog =builder.create();
dialog.show(); }
}

2.实现以下场景:从一个activity中点击一个按钮后,弹出一个单选按钮对话框,上面有“男”“女”两个选项,选定后,TOAST弹出 你选择了男,或你选择了女(参考书上改字体)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="MissingConstraints"
> <TextView
android:id="@+id/v1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="请选择性别:"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:textColor="#FFC0CB"/> <Button
android:id="@+id/v2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择性别"
android:layout_marginTop="20dp"
android:layout_gravity="center" /> </LinearLayout>
package com.example.zuoye;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView textView;
private int[] textSizeArr = {10,20};
int textSize =1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//设置Button监听事件
findViewById(R.id.v2).setOnClickListener(this);
textView=(TextView) findViewById(R.id.v1);
}
@Override
public void onClick(View v){
AlertDialog dialog;
AlertDialog.Builder builder=new AlertDialog.Builder(this)
.setTitle("设置字体大小")//设置标题
.setIcon(R.mipmap.ic_launcher)
.setSingleChoiceItems(new String[]{"男","女"},textSize,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
textSize =which;
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//为textview设置在单选对话框中选择的字体大小
textView.setTextSize(textSizeArr[textSize]);
dialog.dismiss();//关闭对话框
}
})//添加“确定”按钮
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog=builder.create();
dialog.show();
}
}

3.布局(详见:Android第五周上机word文档)

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ADD8E6"
android:layout_margin="2dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/v1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1.TextView显示文本信息"
android:textSize="30sp"
android:textColor="#9370DB"
android:textStyle="bold"
android:gravity="center"
android:layout_marginTop="10dp"
/>
<Button
android:id="@+id/v2"
android:layout_width="340dp"
android:layout_height="wrap_content"
android:text="2.按钮"
android:textSize="20sp"
android:textColor="#9370DB"
android:textStyle="bold"
android:padding="5dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/v1"
/>
<EditText
android:id="@+id/v3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="3.编辑框:请输入信息"
android:textSize="20sp"
android:textColorHint="#9370DB"
android:layout_below="@id/v2"
android:layout_alignStart="@id/v2"
android:layout_margin="5dp"
/>
<RadioGroup
android:id="@+id/v4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/v3"
android:layout_alignLeft="@id/v3"
>
<RadioButton
android:id="@+id/v5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4.男"
android:textColor="#9370DB"
android:textSize="20sp"
android:textStyle="bold"
/>
<RadioButton
android:id="@+id/v6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
android:textColor="#9370DB"
android:textSize="20sp"
android:textStyle="bold"
/>
</RadioGroup>
<LinearLayout
android:id="@+id/v7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="@id/v4"
android:layout_alignLeft="@id/v4">
<CheckBox
android:id="@+id/v8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电脑"
android:textSize="20sp"
/>
<CheckBox
android:id="@+id/v9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机"
android:textSize="20sp"/>
</LinearLayout>
</RelativeLayout>

4.教材p76页 图3—17购物商城界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"> <TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="购物商城"
android:textSize="18sp"
android:textColor="#87CEEB"
android:background="#FFC0CB"
android:gravity="center"
/> <ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"> <ImageView
android:id="@+id/iv"
android:layout_width="120dp"
android:layout_height="90dp"
android:layout_centerVertical="true"/> <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/iv"
android:layout_centerVertical="true"> <TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="桌子"
android:textSize="20sp"
android:textColor="#000000"/> <TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="价格"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:layout_below="@+id/title"
android:textColor="#DDA0DD"/> <TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1000"
android:textSize="20sp"
android:layout_below="@+id/title"
android:layout_toRightOf="@+id/tv_price"
android:textColor="#DDA0DD"
android:layout_marginTop="10dp"/> </RelativeLayout>
</RelativeLayout>
package com.example.cnitcastlistview;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView; public class MainActivity extends Activity {
private ListView mListView;
//商品名称与价格数据集合
public String[] titles ={"桌子","苹果","蛋糕","毛衣"};
private String[] prices ={"1800元","10元/kg","300元","350元"};
//图片数据集合
private int [] icons ={R.drawable.table,R.drawable.apple,R.drawable.cake,R.drawable.wireclotes};
@SuppressLint("WrongViewCast")
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView =(ListView) findViewById(R.id.iv);//初始化ListView控件
MyBaseAdapter mAdapter =new MyBaseAdapter();//创建一个Adapter实例
mListView.setAdapter(mAdapter);//设置Adapter
}
class MyBaseAdapter extends BaseAdapter{ @Override
public int getCount() {//获取item的总数
return titles.length;//返回ListView Item条目的总数
} @Override
public Object getItem(int position) {
return titles[position];//返回Item的数据对象
} @Override
public long getItemId(int position) {
return position;//返回Item的id
}
//得到Item的View视图
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//加载List—item。xml布局文件
View view =View.inflate(MainActivity.this,R.layout.list_item,null);
TextView title =(TextView) view.findViewById(R.id.title);
TextView price =(TextView) view.findViewById(R.id.price);
ImageView iv=(ImageView) view.findViewById(R.id.iv);
title.setText(titles[position]);
price.setText(prices[position]); iv.setBackgroundResource(icons[position]);
return view;
}
} }

Android第五六周作业的更多相关文章

  1. 2018-2019-1 20189221 《Linux内核原理与分析》第六周作业

    2018-2019-1 20189221 <Linux内核原理与分析>第六周作业 实验五 实验过程 将Fork函数移植到Linux的MenuOS fork()函数通过系统调用创建一个与原来 ...

  2. 1903021116—吉琛—Java第六周作业—类的定义

    项目 内容 课程班级博客链接 19信计班 这个作业要求链接 第六周作业链接 java面向对象的概念和定义 博客名称 学号-姓名-Java第六周作业-题目自拟 要求 每道题要有题目,代码(使用插入代码, ...

  3. 201621123080《java程序设计》第六周作业总结

    201621123080<java程序设计>第六周作业总结 1. 本周学习总结 2. 书面作业 clone方法 1.1 在test1包中编写Employee类,在test2包中新建一个Te ...

  4. 2019-2020-1 20199329《Linux内核原理与分析》第六周作业

    <Linux内核原理与分析>第六周作业 一.本周内容概述: 学习系统调用的相关理论知识,并使用库函数API和C代码中嵌入汇编代码两种方式使用getpid()系统调用 学习系统调用syste ...

  5. 2020-2021-1 20209307《Linux内核原理与分析》第六周作业

    这个作业属于哪个课程 <2020-2021-1Linux内核原理与分析)> 这个作业要求在哪里 <2020-2021-1Linux内核原理与分析第六周作业> 这个作业的目标 & ...

  6. C语言--第六周作业评分和总结(5班)

    作业链接:https://edu.cnblogs.com/campus/hljkj/CS2017-5/homework/1250 一.评分要求 要求1 完成PTA第六周所有题,若存在抄袭现象,倒扣此题 ...

  7. 1903021121—刘明伟—Java第六周作业—java类

    项目   内容 课程班级博客链接  19信计班(本) 作业要求链接 第6周作业 扩展阅读 java面向对象的概念和定义 作业要求 每道题要有题目,代码,截图(只截运行结果). 题目1: 一个Phone ...

  8. 201621123062《java程序设计》第六周作业总结

    1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰 ...

  9. 2019春第六周作业Compile Summarize

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 在这里 我在这个课程的目标是 能够熟练掌握指针的用法 这个作业在那个具体方面帮助我实现目标 对指针的使用更加得心应手 参考文献与网址 C语 ...

随机推荐

  1. Mysql常用操作笔记

    目录 登录 退出 Sql语句分类 DDL操作数据库 1.创建数据库 2.查看数据库 3.修改数据库 4.删除数据库 5.使用数据库 6.创建表 7.查看表 8.删除表 9.修改表 10.常用字段类型 ...

  2. 解决Flash CS6初始化字体就自动退出

    系统win7 问题始于安装了同事的字体,启动Flash CS6时界面上显示开始初始化字体...然后 就自动退出了 尝试过进入pe系统删除字体,可是删除不了 网上搜索找到了这一个删除字体的工具FontF ...

  3. 你也可以很硬核「GitHub 热点速览 v.22.13」

    本周特推介绍了一个非常易上手,操作难度(主要难度在于机件购买)极低的硬件项目,SmartKnob 让你有个可玩性极高的控制仪.本周特推另外一个项目则是一个安全项目,打破你对 URL 是可靠.安全的认知 ...

  4. JAVA 用命令提示符执行java找不到或者无法加载主类

    使用cmd编译执行java文件时候,报错---找不到或者无法加载主类如下图 把红色部分去掉,再次编译执行如下解决问题 ,执行成功!!!!!! 2.当我们在eclipes中执行运行的时候 ggggggg ...

  5. Servlet:浏览器下载文件时文件名为乱码问题

    1 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletExcep ...

  6. 在Spring框架中如何更有效地使用JDBC?

    使用SpringJDBC 框架,资源管理和错误处理的代价都会被减轻.所以开发者只需写statements 和 queries从数据存取数据,JDBC也可以在Spring框架提供的模板类的帮助下更有效地 ...

  7. Mysql的索引及优化

    一:四种存储引擎: mysql使用 show engines查询其存储引擎: 功  能 MYISAM Memory InnoDB Archive 存储限制 256TB RAM 64TB None 支持 ...

  8. 集合迭代器Iterator

    迭代器模式:就是提供一种方法对一个容器对象中的各个元素进行访问,而又不暴露该对象容器的内部细节. 什么是迭代器Iterator? Java集合框架的集合类,我们有时候称之为容器.容器的种类有很多种,比 ...

  9. AOP 有哪些实现方式?

    实现 AOP 的技术,主要分为两大类: 静态代理 指使用 AOP 框架提供的命令进行编译,从而在编译阶段就可生成 AOP 代理类, 因此也称为编译时增强: 编译时编织(特殊编译器实现) 类加载时编织( ...

  10. RocketMQ实现分布式事务

    相关文章:http://www.uml.org.cn/zjjs/201810091.asp(深入理解分布式事务,高并发下分布式事务的解决方案) 三种分布式事务: 1.基于XA协议的两阶段提交 2.消息 ...