文章目录
- 一、案例演示
- 二、项目构成
- 三、实现代码
- 1、activity_main.xml
- 2、MainActivity.java
- item_spinner.xml
- textSize.xml
- 2.1、text1() 查看头像
- 2.1.1、dialog_picture.xml
- 2.2、text2() 修改名称
- 2.2.1、dialog_username.xml
- 2.3、text3() 修改学号
- 2.3.1、dialog_student_id.xml
- 2.4、text4() 修改手机号
- 2.4.1、dialog_phone.xml
- 2.5、text5() 性别选择
- 2.6、text6() 生日选择
- 2.7、text9() 爱好选择
- 3、styles.xml
一、案例演示
整体线形垂直布局,局部相对布局,名称、学号、电话使用自定义对话框,性别、爱好分别用单选、多选对话框学院,学习状态用下拉框。
二、项目构成
三、实现代码
1、activity_main.xml
<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:background="@color/hui"android:orientation="vertical"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#ffffff"><ImageViewandroid:layout_width="60dp"android:layout_height="60dp"android:background="@drawable/left"></ImageView><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="60dp"android:text="我的信息"android:textSize="40dp" /></LinearLayout><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:background="#ffffff"android:orientation="vertical"><RelativeLayoutandroid:id="@+id/picture"style="@style/relativelayout"android:layout_marginTop="20dp"><TextViewandroid:id="@+id/text1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:text="头像"android:textSize="30dp"></TextView><TextViewandroid:id="@+id/view1"android:layout_width="40dp"android:layout_height="40dp"android:layout_centerVertical="true"android:layout_marginRight="10dp"android:layout_toLeftOf="@id/imageView1"android:background="@drawable/lufei"></TextView><ImageViewandroid:id="@+id/imageView1"android:layout_width="40dp"android:layout_height="50dp"android:layout_alignParentRight="true"android:layout_centerVertical="true"app:srcCompat="@drawable/right" /></RelativeLayout><TextView style="@style/line"></TextView><RelativeLayoutandroid:id="@+id/username"style="@style/relativelayout"><TextViewandroid:id="@+id/text2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:text="呢称"android:textSize="30dp"></TextView><TextViewandroid:id="@+id/view2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:layout_toLeftOf="@id/imageView2"android:text="坤坤"android:textSize="25dp"></TextView><ImageViewandroid:id="@+id/imageView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"app:srcCompat="@drawable/love" /></RelativeLayout><TextView style="@style/line"></TextView><RelativeLayoutandroid:id="@+id/student_id"style="@style/relativelayout"><TextViewandroid:id="@+id/text3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:text="学号"android:textSize="30dp"></TextView><TextViewandroid:id="@+id/view3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:layout_toLeftOf="@id/imageView3"android:text="202030120099"android:textSize="25dp"></TextView><ImageViewandroid:id="@+id/imageView3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"app:srcCompat="@drawable/tree" /></RelativeLayout><TextView style="@style/line"></TextView><RelativeLayoutandroid:id="@+id/phone"style="@style/relativelayout"><TextViewandroid:id="@+id/text4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:text="电话"android:textSize="30dp"></TextView><TextViewandroid:id="@+id/view4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:layout_toLeftOf="@id/imageView4"android:text="15953613166"android:textSize="25dp"></TextView><ImageViewandroid:id="@+id/imageView4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"app:srcCompat="@drawable/yao" /></RelativeLayout><TextView style="@style/line"></TextView><RelativeLayoutandroid:id="@+id/sex"style="@style/relativelayout"><TextViewandroid:id="@+id/text5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:text="性别"android:textSize="30dp"></TextView><TextViewandroid:id="@+id/view5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:layout_toLeftOf="@id/imageView5"android:text="男"android:textSize="25dp"></TextView><ImageViewandroid:id="@+id/imageView5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"app:srcCompat="@drawable/love" /></RelativeLayout><TextView style="@style/line"></TextView><RelativeLayoutandroid:id="@+id/birthday"style="@style/relativelayout"><TextViewandroid:id="@+id/text6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:text="生日"android:textSize="30dp"></TextView><TextViewandroid:id="@+id/view6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:layout_toLeftOf="@id/imageView6"android:text="2001.9.9"android:textSize="25dp"></TextView><ImageViewandroid:id="@+id/imageView6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"app:srcCompat="@drawable/kiss" /></RelativeLayout><TextView style="@style/line"></TextView><RelativeLayoutandroid:id="@+id/college"style="@style/relativelayout"><TextViewandroid:id="@+id/text7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:text="学院"android:textSize="30dp"></TextView><Spinnerandroid:id="@+id/spinner_college"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:spinnerMode="dropdown"></Spinner></RelativeLayout></LinearLayout><LinearLayoutandroid:layout_marginTop="20dp"android:background="#ffffff"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><RelativeLayoutandroid:id="@+id/holiday"style="@style/relativelayout"><TextViewandroid:id="@+id/text8"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:text="学习状态"android:textSize="30dp"></TextView><Spinnerandroid:id="@+id/spinner_holiday"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerInParent="true"android:spinnerMode="dialog"></Spinner></RelativeLayout><TextView style="@style/line"></TextView><RelativeLayoutandroid:id="@+id/hobby"style="@style/relativelayout"><TextViewandroid:id="@+id/text9"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:text="爱好"android:textSize="30dp"></TextView><TextViewandroid:id="@+id/view9"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:layout_toLeftOf="@id/imageView9"android:text="唱"android:textSize="25dp"></TextView><ImageViewandroid:id="@+id/imageView9"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"app:srcCompat="@drawable/love" /></RelativeLayout></LinearLayout></LinearLayout>
2、MainActivity.java
package com.example.information;import androidx.appcompat.app.AlertDialog;import androidx.appcompat.app.AppCompatActivity;import android.app.DatePickerDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.widget.Adapter;import android.widget.ArrayAdapter;import android.widget.DatePicker;import android.widget.EditText;import android.widget.RelativeLayout;import android.widget.SimpleAdapter;import android.widget.Spinner;import android.widget.TextView;import java.util.ArrayList;import java.util.Calendar;import java.util.HashMap;import java.util.List;import java.util.Map;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private List<Map<String,Object>> data;//一个集合//查看头像private RelativeLayout picture;//名称private RelativeLayout username;private TextView view2;//学号private RelativeLayout student_id;private TextView view3;//修改手机号private RelativeLayout phone;private TextView view4;//选择性别private RelativeLayout sex;private TextView view5;//生日private RelativeLayout birthday;private TextView view6;//学院private RelativeLayout college;private Spinner spinner;//状态private RelativeLayout holiday;private Spinner spinner2;//爱好private RelativeLayout hobby;private TextView view9;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}public void initView(){//找到对应控件picture=findViewById(R.id.picture);username=findViewById(R.id.username);view2=findViewById(R.id.view2);student_id=findViewById(R.id.student_id);view3=findViewById(R.id.view3);phone=findViewById(R.id.phone);view4=findViewById(R.id.view4);sex=findViewById(R.id.sex);view5=findViewById(R.id.view5);birthday=findViewById(R.id.birthday);view6=findViewById(R.id.view6);college=findViewById(R.id.college);spinner=findViewById(R.id.spinner_college);holiday=findViewById(R.id.holiday);spinner2=findViewById(R.id.spinner_holiday);hobby=findViewById(R.id.hobby);view9=findViewById(R.id.view9);//设置事件监听picture.setOnClickListener(this);username.setOnClickListener(this);student_id.setOnClickListener(this);phone.setOnClickListener(this);sex.setOnClickListener(this);birthday.setOnClickListener(this);college.setOnClickListener(this);holiday.setOnClickListener(this);hobby.setOnClickListener(this);//学院选择String arr[]=new String[]{"智慧学院","教育学院","艺术学院"};ArrayAdapter a=new ArrayAdapter(this,R.layout.textsize,arr);spinner.setAdapter(a);//状态选择data = new ArrayList<>();SimpleAdapter adapter = new SimpleAdapter(this, getDat(), R.layout.item_spinner,new String[]{"image", "text"}, new int[]{R.id.img, R.id.tvv});spinner2.setAdapter(adapter);}private List<Map<String,Object>> getDat() {Map<String, Object> map = new HashMap<>();map.put("image", R.drawable.dujia);map.put("text", "度假中");data.add(map);Map<String, Object> map1 = new HashMap<>();map1.put("image", R.drawable.kiss);map1.put("text", "工作中");data.add(map1);Map<String, Object> map2 = new HashMap<>();map2.put("image", R.drawable.yao);map2.put("text", "会议中");data.add(map2);Map<String, Object> map3 = new HashMap<>();map3.put("image", R.drawable.tree);map3.put("text", "出差中");data.add(map3);return data;}@Overridepublic void onClick(View v) {switch (v.getId()){case R.id.picture:text1();break;case R.id.username:text2();break;case R.id.student_id:text3();break;case R.id.phone:text4();break;case R.id.sex:text5();break;case R.id.birthday:text6();break;case R.id.hobby:text9();break;}}}
item_spinner.xml
设置图片布局,修改状态时用到
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_margin="5dp"android:layout_width="match_parent"android:layout_height="wrap_content"><ImageViewandroid:background="@mipmap/ic_launcher"android:id="@+id/img"android:layout_width="40dp"android:layout_height="40dp"/><TextViewandroid:layout_marginLeft="10dp"android:layout_gravity="center_vertical"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="度假中"android:textSize="23sp"android:id="@+id/tvv"/></LinearLayout>
textSize.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@android:id/text1"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="25dp"android:textAppearance="?android:attr/textAppearanceListItemSmall"android:gravity="center_vertical"android:paddingStart="?android:attr/listPreferredItemPaddingStart"android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"android:minHeight="?android:attr/listPreferredItemHeightSmall" />
2.1、text1() 查看头像
//查看头像public void text1(){//获取内容View layout =View.inflate(this,R.layout.dialog_picture,null);//创建对象AlertDialog.Builder b=new AlertDialog.Builder(this);//修改标题b.setTitle("查看头像");//赋值内容设置窗口b.setView(layout);//修改b.setPositiveButton("确定",null);b.setNegativeButton("取消",null);b.show();}
2.1.1、dialog_picture.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/view1"android:layout_width="100dp"android:layout_height="100dp"android:layout_centerInParent="true"android:background="@drawable/lufei"></TextView></RelativeLayout>
2.2、text2() 修改名称
//修改名称public void text2(){//获取内容View layout =View.inflate(this,R.layout.dialog_username,null);//创建对象AlertDialog.Builder b=new AlertDialog.Builder(this);//修改标题b.setTitle("修改名称");//赋值内容设置窗口b.setView(layout);//修改b.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {EditText update=layout.findViewById(R.id.view2);String newName=update.getText().toString().trim();//设置数据view2.setText(newName);}});b.setNegativeButton("取消",null);b.show();}
2.2.1、dialog_username.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><EditTextandroid:id="@+id/view2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_centerInParent="true"android:text="坤坤"android:textSize="25dp"></EditText></RelativeLayout>
2.3、text3() 修改学号
//修改学号public void text3(){//获取内容View layout =View.inflate(this,R.layout.dialog_student_id,null);//创建对象AlertDialog.Builder b=new AlertDialog.Builder(this);//修改标题b.setTitle("修改学号");//赋值内容设置窗口b.setView(layout);//修改b.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {EditText update=layout.findViewById(R.id.view3);String newId=update.getText().toString().trim();//设置数据view3.setText(newId);}});b.setNegativeButton("取消",null);b.show();}
2.3.1、dialog_student_id.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><EditTextandroid:id="@+id/view3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_centerInParent="true"android:text="202030120099"android:textSize="25dp"></EditText></RelativeLayout>
2.4、text4() 修改手机号
//修改手机号public void text4(){//获取内容View layout =View.inflate(this,R.layout.dialog_phone,null);//创建对象AlertDialog.Builder b=new AlertDialog.Builder(this);//修改标题b.setTitle("修改手机号");//赋值内容设置窗口b.setView(layout);//修改b.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {EditText update=layout.findViewById(R.id.view4);String newPhone=update.getText().toString().trim();//设置数据view4.setText(newPhone);}});b.setNegativeButton("取消",null);b.show();}
2.4.1、dialog_phone.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><EditTextandroid:id="@+id/view4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_centerInParent="true"android:text="15953613166"android:textSize="25dp"></EditText></RelativeLayout>
2.5、text5() 性别选择
//性别选择public void text5(){String arr[] ={"男","女"};AlertDialog.Builder b=new AlertDialog.Builder(this);b.setTitle("选择性别");b.setSingleChoiceItems(arr, 0, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {view5.setText(arr[which]);dialog.dismiss();}});b.show();}
2.6、text6() 生日选择
//生日选择public void text6(){//获取日期Calendar c= Calendar.getInstance();int year=c.get(Calendar.YEAR);int month=c.get(Calendar.MONTH);int day=c.get(Calendar.DAY_OF_MONTH);DatePickerDialog d=new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener(){@Overridepublic void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {view6.setText(year+"."+month+"."+dayOfMonth);}},year,month,day);d.show();}
2.7、text9() 爱好选择
//爱好public void text9(){ArrayList<Integer> list=new ArrayList();String arr[]={"唱","跳","rap","篮球"};boolean fff[]={false,false,false,false};AlertDialog.Builder b=new AlertDialog.Builder(this);b.setTitle("爱好选择");b.setMultiChoiceItems(arr, fff, new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {if(isChecked){list.add(which);}else {list.remove(which);}}});b.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {String str="";for(int i=0;i<list.size();i++){str += arr[list.get(i)]+"";}view9.setText(str);}});b.setNegativeButton("取消",null);b.show();}
3、styles.xml
共用的资源
<resources><style name="relativelayout"><item name="android:layout_height">0dp<item name="android:layout_weight">1<item name="android:layout_width">wrap_content<item name="android:layout_marginLeft">20dp<item name="android:layout_marginRight">20dp</style><style name="line"><item name="android:layout_width">match_parent<item name="android:layout_height">1dp<item name="android:layout_marginRight">20dp<item name="android:layout_marginLeft">20dp<item name="android:background">@color/black</style></resources>