Android Studio学习记录之简单的页面切换及宫格菜单
Android Studio学习记录之简单的页面切换及宫格菜单
之前上课听老师讲的一些东西自己其实并没有消化,今天把不懂的都去网上搜了一下,有了一种恍然大悟的感觉,包括很多方方面面的东西。有些东西听说了也明白,但是没有实际的操作就还是不能够知道具体场合应该怎么用。虽然不能面面俱到把所有的东西都搞透,但是通过整理的过程,希望把我理解的东西表达出来,也希望以后回过来看的时候,能够有进一步的理解。
1.首先是MainActivity.java,一个主界面
public class MainActivity extends AppCompatActivity implements View.OnClickListener{private static final String TAG = "MainActivity";//定义静态常量private Button button,button1;private EditText editText;//Activity初始化顺序,onCreate(),onStart(),onReaume()@Overrideprotected void onCreate(Bundle savedInstanceState) {//保护当时的状态,以防被kill掉super.onCreate(savedInstanceState);Log.d(TAG, "onCreate() called with: savedInstanceState = [" + savedInstanceState + "]");setContentView(R.layout.activity_main);//登录button = (Button)findViewById(R.id.button);//注册button1 = (Button)findViewById(R.id.button2);editText = (EditText)findViewById(R.id.editText5);button.setOnClickListener(this);button1.setOnClickListener(this);}@Overrideprotected void onStart() {super.onStart();Log.d(TAG, "onStart() called");}@Overrideprotected void onResume() {super.onResume();Log.d(TAG, "onResume() called");}//Activity销毁顺序:onPause(),onStop(),onDestroy()@Overrideprotected void onPause() {super.onPause();Log.d(TAG, "onPause() called");}@Overrideprotected void onRestart() {super.onRestart();Log.d(TAG, "onRestart() called");}@Overrideprotected void onDestroy() {super.onDestroy();Log.d(TAG, "onDestroy() called");}@Override//可以将多个View的onClick事件写到一个onClick事件中,当事件发生时,参数v就指向当前点击的Viewpublic void onClick(View v) {String show = editText.getEditableText().toString();//Intent:意图(一个Activity启动另一个Activity,需要startActivity()函数,函数的参数是Intent对象)//取Intent对象//初始化Intent intent = getIntent();switch (v.getId()){case R.id.button: //登录 R文件下的//设置需要跳转的activityintent.setClass(this,LoginActivity.class);//bundle对象是用来数据传递//创建bundle对象Bundle bundle = new Bundle();//把需要传的数据放入bundlebundle.putString("show",show);//把bundle放入intent(进行封装)intent.putExtras(bundle);//跳转startActivity(intent);break;case R.id.button2://注册intent.setClass(this,RegisterActivity.class);startActivity(intent);break;}}
}
这次就是比昨天更进一步理解了Activity的生命周期,以及为何继承AppCompatActivity,这个会比Activity多一个标签,有一些区别。
样式也放一下
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns: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:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin"tools:context="com.example.k42.myapplication.MainActivity"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"app:srcCompat="@drawable/lux"android:id="@+id/imageView"android:layout_alignParentTop="true"android:layout_alignParentStart="true" /><Buttonandroid:text="注册"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBottom="@+id/button"android:layout_alignParentEnd="true"android:layout_marginEnd="64dp"android:id="@+id/button2" /><Buttonandroid:text="登录"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="27dp"android:id="@+id/button"android:layout_alignParentBottom="true"android:layout_toStartOf="@+id/button2"android:layout_marginEnd="24dp" /><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:inputType="textPersonName"android:hint="请输入学校"android:ems="10"android:layout_above="@+id/button2"android:layout_alignStart="@+id/button"android:layout_marginBottom="16dp"android:id="@+id/editText5" />
</RelativeLayout>
效果图
2. 登录注册
这个也没啥好提的,只是我要借助这个页面跳转到菜单,下面是代码
RegisterActivity.java
public class RegisterActivity extends Activity implements View.OnClickListener{private static final String TAG = "RegisterActivity";private EditText editText,editText1,editText2;private Button button,button1;private String name,password,phone;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.register);Log.d(TAG, "onCreate() called ");editText = (EditText)findViewById(R.id.editText);editText1 = (EditText)findViewById(R.id.editText1);editText2 = (EditText)findViewById(R.id.editText2);button = (Button)findViewById(R.id.button5);button1 = (Button)findViewById(R.id.button6);//给button按钮设置单击事件的监听器button.setOnClickListener(this);button1.setOnClickListener(this);}/*** 监听单击事件的方法* @param v*/@Overridepublic void onClick(View v) {Log.d(TAG, "onClick() called with: v = [" + v + "]");switch (v.getId()){case R.id.button5: //点击的是确认按钮name = editText.getText().toString();password = editText1.getText().toString();phone = editText2.getText().toString();Log.d(TAG, name+","+password+","+phone);//显示提示信息//第一个参数: 上下文,当前类//第二个参数: 显示的提示信息//第三个参数: 显示的时间长短Toast.makeText(this,name+","+password+","+phone,Toast.LENGTH_SHORT).show();break;case R.id.button6: //点击的是返回按钮break;}}
}
样式register.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:text="注册"android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/textView2"android:layout_gravity="center"android:textSize="30sp"android:layout_marginTop="10dp"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="姓名:"android:layout_marginLeft="10dp"/><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/editText"android:hint="请输入姓名"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密码:"android:layout_marginLeft="10dp"/><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/editText1"android:hint="请输入密码"android:inputType="textPassword"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="手机:"android:layout_marginLeft="10dp"/><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/editText2"android:hint="请输入手机"android:inputType="phone"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="确定"android:id="@+id/button5"android:background="@color/colorPrimary"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="返回"android:id="@+id/button6"/></LinearLayout></LinearLayout>
效果图
登录Login.java
public class LoginActivity extends Activity implements View.OnClickListener{private Button button,button1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.login);button = (Button)findViewById(R.id.button7);button1 = (Button)findViewById(R.id.button8);button.setOnClickListener(this);button1.setOnClickListener(this);//取intent对象Intent intent = getIntent();//取bundle对象Bundle bundle = intent.getExtras();//从bundle对象中取数据String show = bundle.getString("show");Toast.makeText(this,show,Toast.LENGTH_SHORT).show();}@Overridepublic void onClick(View v) {Intent intent = getIntent();switch (v.getId()){case R.id.button7:intent.setClass(this,MenuActivity.class);startActivity(intent);break;case R.id.button8:intent.setClass(this,MainActivity.class);startActivity(intent);break;}}
}
样式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:text="登录"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:textSize="30sp"android:layout_marginTop="10dp"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="用户名:"android:layout_marginLeft="10dp"/><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/editText3"android:hint="请输入用户名"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密码:"android:layout_marginLeft="10dp"/><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/editText4"android:hint="请输入密码"android:inputType="textPassword"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="确定"android:id="@+id/button7"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="返回"android:id="@+id/button8"/></LinearLayout></LinearLayout>
效果图
3.这次的重点是菜单
这一块由三个部分组成,用的是MVC模式,即model,view,control.下面的代码中 menu.xml负责菜单界面的布局,grid_item.xml负责单个图标的显示样式,MenuActivity.java中的Adapter适配器负责控制。
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><!--numColumns: 每一行多少个horizontalSpacing:第一行的每个单元格之间的间距verticalSpacing: 行间距--><GridViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:numColumns="3"android:layout_marginTop="10dp"android:horizontalSpacing="20dp"android:verticalSpacing="20dp"android:gravity="center"android:id="@+id/grid_view"/>
</LinearLayout>
注:这里的gridview是表示宫格显示。
效果图
grid_item.xml
<?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"android:layout_width="match_parent" android:layout_height="match_parent"><ImageViewandroid:layout_width="60dp"android:layout_height="60dp"app:srcCompat="@drawable/a"android:layout_centerHorizontal="true"android:id="@+id/imageView2" /><TextViewandroid:text="TextView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/imageView2"android:layout_marginTop="5dp"android:layout_centerHorizontal="true"android:id="@+id/textView3" />
</RelativeLayout>
效果图
MenuActivity.java
public class MenuActivity extends Activity implements AdapterView.OnItemClickListener{private static final String TAG = "MenuActivity";//gridView宫格形式private GridView gridView;//定义菜单项的图标private int[] icons = {R.drawable.menu1,R.drawable.menu2,R.drawable.menu3,R.drawable.menu4,R.drawable.menu5,R.drawable.menu6};//定义菜单项的标题private String[] titles = {"宠物","花花","新闻","天气","游戏","音乐"};//定义一个适配器private SimpleAdapter simpleAdapter;//gridview需要显示的所有的菜单数据private List<Map<String,Object>> data;@Override//重写父类的方法protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.menu);//findViewById最终调用ViewGroup中的findViewTraversal,这个方法会遍历所有的子ViewgridView = (GridView)findViewById(R.id.grid_view);init();//第一个参数: 上下文//第二个参数: 需要显示的数据//第三个参数:每一个选项显示的布局文件//第四个参数: list中hashmap的键构造的数组//第五个参数: 第四个参数数组中的键的值对应的需要显示的组件IDsimpleAdapter = new SimpleAdapter(this,data,R.layout.grid_item,new String[]{"icon","title"},new int[]{R.id.imageView2,R.id.textView3});//绑定适配器gridView.setAdapter(simpleAdapter);//设置监听器gridView.setOnItemClickListener(this);}/*** 初始化菜单项的数据*/public void init(){data = new ArrayList<>();//动态数组//遍历图标数组for(int i=0;i<icons.length;i++){//构造每一个菜单项的HashMapMap<String,Object> item = new HashMap<>();item.put("icon",icons[i]);item.put("title",titles[i]);data.add(item);}Log.d(TAG, data.toString());}/*** 每一个选项点击事件触发的方法* @param parent* @param view* @param position* @param id*/@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {Intent intent = getIntent();switch (position) {case 0:intent.setClass(this,PetActivity.class);startActivity(intent);break;case 1:intent.setClass(this,FlowerActivity.class);startActivity(intent);break;case 2:break;case 3:break;case 4:break;case 5:break;}}
}
终极效果
使用的是海马玩的模拟器,其中可以发现在MenuActivity中的HashMap是用来遍历每一组图标和标题的。
4.点击图标后做出回应
我们以第一个图标为例,点击宠物,使它做出回应。
public class PetActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//提示信息Toast.makeText(this,"这是宠物",Toast.LENGTH_SHORT).show();}
}
至此,关于菜单部分的整理结束了。
其中有一点要特别说一下,每做一个页面,要让它能够显示出来,一定要去main下的AndroidManifest.xml中注册一下(如下代码)
<activity android:name=".PetActivity" />
否则,在模拟器上会显示
完整部分如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.k42.myapplication" ><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:supportsRtl="true"android:theme="@style/AppTheme" ><activity android:name=".MainActivity" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><!-- 注册activity --><activity android:name=".LoginActivity" /><activity android:name=".RegisterActivity" /><activity android:name=".MenuActivity" /><activity android:name=".PetActivity" /></application></manifest>
Android Studio学习记录之简单的页面切换及宫格菜单相关推荐
- Android Studio 学习记录-图形定制
目录 图形Drawable 形状图形 1.shape(形状) 2.size(尺寸) 3.stroke(描边) 4.corners(圆角) 5.solid(填充) 6.padding(间隔) 7.gra ...
- Android Studio 学习实例记录-手电筒
Android Studio 学习实例记录-手电筒 刚安装好Android Studio3.1.2,上网搜了一个实例进行实践学习,仅用这篇文章来记录学习过程 MainActivity.java的源码来 ...
- Android Studio --- [学习笔记]RadioButton、CheckBox、ImageView、ListView、TCP的三次握手
说明 源代码 在2.x里有TCP的三次挥手与四次握手,先对它进行简单的回答(百度).预计在下一篇里,会继续说明TCP 接上一篇: Android Studio - > [学习笔记]Button. ...
- (超多图)基于Android studio开发的一个简单入门小应用(超级详细!!)(建议收藏)
基于Android studio开发的一个简单入门小应用 一.前言 二.前期准备 三.开发一个小应用 五.运行应用 一.前言 在暑假期间,我学习JAVA基础,为了能早日实现自己用代码写出一个app的& ...
- Android动画学习记录一(Android动画种类、补间动画和帧动画)
Android动画学习记录一(动画种类.补间动画和帧动画) 动画种类.补间动画和帧动画 Android动画学习记录一(动画种类.补间动画和帧动画) 一.动画种类 二.View动画 2.1 补间动画 补 ...
- Android Studio --- [学习笔记]TCP(第2弹)、GridView、ScrollView
说明 这篇主要接上一篇Android Studio - > [学习笔记]RadioButton.CheckBox.ImageView.ListView.TCP的三次握手 对上面回答的细解,并用J ...
- Android Studio 使用记录
Android Studio 使用记录 工具及教程http://www.android-studio.org/ Android Studio简单设置http://ask.android-studio. ...
- Android动画学习记录二(属性动画、估值器和插值器)
Android动画学习记录二(属性动画.估值期和插值器) Android动画学习记录二(属性动画.估值期和插值器) Android动画学习记录二(属性动画.估值期和插值器) 一.补间动画缺陷 二.属性 ...
- android studio gradle 添加jar,android studio学习----通过gradle来导入jar包
转载地址:http://www.th7.cn/Program/Android/201507/495477.shtml File->Project Structure 可以打开下面的图: 1.通过 ...
最新文章
- 用bert来训练quoras question pairs的代码仓
- python DataFrame join()
- how to improve efficiency of graphic neural network?
- c语言p1口转向灯实验,实验三模拟汽车左右转向灯控制.doc
- linux 信号量锁 内核,Linux内核信号量互斥锁应用
- ACM训练计划(上)
- python在线投票系统讲解_在线投票系统功能分析
- Django框架(展示图书信息简易版)
- LeetCode MySQL 1667. 修复表中的名字
- 数据封装以及解封的过程
- 无人驾驶(再谈基于camera的高精度地图)
- vue 时间插件_Vue插件丨vxe-table初体验
- How to enable/disable EWF
- SPSS 产生正交数据集
- 计算机重新启动后打印机脱机,重新启动计算机后打印机脱机怎么办
- 渐进式jpg转换成基线式 jpg
- 算法艺术(一):Hello world
- snmpwalk命令常用方法总结
- ImageGP/BIC无代码绘制差异基因火山图
- 国内首届中文人机对话技术评测赛果出炉,两项任务冠军团队都分享了哪些技术细节?...
热门文章
- IPMI远程管理服务器时,不要改动 主板显卡为 Auto 或 禁用
- 树莓派小车实现目标追踪(coco数据集,gluoncv,树莓派和PC信息交互)
- 联想y7000p怎么连接显示器_内存、硬盘不够用?手把手教你升级联想拯救者Y7000P...
- 电子邮件推广方法,学会就赚到了
- vue返回上一页不刷新页面的方法 / vue缓存页面
- 夸克真的实用吗,其实除了它还有更好用的浏览器
- 阿联酋外汇业务的监管宽松?一篇看懂如何在阿联酋做外汇交易!
- ios13怎么打开科学计算机,Apple支援:iOS 13 分屏如何操作?iOS 13 分屏显示在哪设置开启?...
- Tomcat 控制台 乱码 淇℃伅
- js实现深度遍历与广度遍历