<7.22>新的启航 Android学习笔记(二)
来源:互联网 发布:心理咨询师网络课程 编辑:程序博客网 时间:2024/06/05 10:15
9:40正式开始学习
今天目标:五节课的内容
继续加油!
第六课:常见控件的使用方法
一、TextView的使用方法
二、EditText的使用方法
三、Button的使用方法
四、Menu的使用方法
附练习代码:
MainActivity:
<span style="font-family:Microsoft YaHei;font-size:18px;">package com.yrh.tebm;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity {private Button caculate;private EditText factorOne;private EditText factorTwo;private TextView symbol;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 通过id获得对应控件的对象caculate = (Button) findViewById(R.id.caculate);factorOne = (EditText) findViewById(R.id.factorOne);factorTwo = (EditText) findViewById(R.id.factorTwo);symbol = (TextView) findViewById(R.id.symbol);// 设置内容caculate.setText("计算");symbol.setText("乘以");factorOne.setHint("乘数");factorTwo.setHint("被乘数");// 设置监听器caculate.setOnClickListener(new caculateOnClickListener());}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {menu.add(0, 1, 1, "退出");menu.add(0, 2, 2, "关于");return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onContextItemSelected(MenuItem item) {if (1 == item.getItemId()) {finish();}return super.onContextItemSelected(item);}class caculateOnClickListener implements OnClickListener {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString factorOneString = factorOne.getText().toString();String factorTwoString = factorTwo.getText().toString();Intent intent = new Intent();intent.putExtra("one", factorOneString);intent.putExtra("two", factorTwoString);intent.setClass(MainActivity.this, ResultActivity.class);startActivity(intent);}}}</span>ResultActivity:
<span style="font-family:Microsoft YaHei;font-size:18px;">package com.yrh.tebm;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class ResultActivity extends Activity {private TextView resultTextView;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_result);resultTextView = (TextView) findViewById(R.id.result);Intent intent = getIntent();String factorOneStr = intent.getStringExtra("one");String factorTwoStr = intent.getStringExtra("two");int factorOneInt = Integer.parseInt(factorOneStr);int factorTwoInt = Integer.parseInt(factorTwoStr);int result = factorOneInt * factorTwoInt;resultTextView.setText(result + "");}}</span>
第七课:Activity的生命周期(一)
(1)protected void onCreate(Bundle savedInstanceState);
(2)protected void onStart();
(3)protected void onRestart();
(4)protected void onResume();
(5)protected void onPause();
(6)protected void onStop();
(7)protected void onDestroy();
二、有两个Activity:
FirstActivity、SecondActivity
当第一个Activity启动时
依次调用:
(1)FirstActivity-onCreate
(2)FirstActivity-onStart
(3)FirstActivity-onResume
从第一个Activity启动第二个Activity时
依次调用:
(1)FirstActivity-onPause
(2)SecondActivity-onCreate
SecondActivity-onStart
SecondActivity-onResume
(3)FirstActivity-onStop
当从第二个Activity回到第一个Activity时
依次调用:
(1)SecondActivity-onPause
(2)FirstActivity-onRestart
FirstActivity-onStart
FirstActivity-onResume
(3)SecondActivity-onStop
SecondActivity-onDestory
再从第一个Activity启动第二个Activity时
依次调用:
(1)FirstActivity-onPause
(2)SecondActivity-onCreate
SecondActivity-onStart
SecondActivity-onResume
(3)FirstActivity-onStop
第八课:Activity的生命周期(二)
(1)什么是Task?
Task就是一个栈,里面存放了很多Activity
| 第三个Activity |
|___________|
| 第二个Activity |
|___________|
| 第一个Activity |
|___________|
(2)Task的作用:
不同应用程序的Activity利用Task可以组织成一个单元,提高用户的体验。
(3)Task栈中Activity的顺序是不能改变的
二、Activity和Task之间的关系
(1)Task运行的过程
1)应用程序启动之后,运行第一个Activity之后,该Activity对象被压入到Stack当中。
| FirstActivity | <-
|_________|
2)点击按钮后启动第二个Activity,该Activity对象被压入到Stack当中。
| SecondActivity | <-
| FirstActivity |
|___________|
3)点击第二个Activity的按钮之后,启动第三个Activity,该Activity对象被压入到Stack当中。
| ThirdActivity | <-
| SecondActivity |
| FirstActivity |
|___________|
4)点击第三个Activity的按钮之后,启动第四个Activity。
| SMS Activity | <-
| ThirdActivity |
| SecondActivity |
| FirstActivity |
|___________|
5)点击Back按钮,这时SMS Activity对象从栈中弹出
| ThirdActivity | -> SMS Activity
| SecondActivity |
| FirstActivity |
|___________|
三、对话框风格Activity的使用方法
(1)在AndroidManifest.xml文件中指定一下风格:
android:name="@android:style/Theme.Dialog"
附练习代码:
MainActivity:
<span style="font-family:Microsoft YaHei;font-size:18px;">package com.yrh.dialogactivity;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {private TextView textView;private Button button1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);textView = (TextView) findViewById(R.id.textView);button1 = (Button) findViewById(R.id.button1);textView.setText("Hello Wolrd");button1.setText("进入DialogActivity");button1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();intent.setClass(MainActivity.this, DialogActivity.class);startActivity(intent);}});}}</span>
DialogActivity:
<span style="font-family:Microsoft YaHei;font-size:18px;">package com.yrh.dialogactivity;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class DialogActivity extends Activity{private Button goBack;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_dialog);goBack = (Button)findViewById(R.id.goBack);goBack.setText("回到第一个Activity");goBack.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {finish();}});}}</span>
AndroidManifest.xml
<span style="font-family:Microsoft YaHei;font-size:18px;"><?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yrh.dialogactivity" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.yrh.dialogactivity.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.yrh.dialogactivity.DialogActivity" android:label="窗口Activity" android:theme="@android:style/Theme.Dialog" > </activity> </application></manifest></span>
第九课:Activity的布局初步(一)
一、LinearLayout的使用方法
(1)重要属性:
1)android:orientation方向属性
值: horizontal 水平的
vertical 垂直的
附练习代码:
linearlayout:
<span style="font-family:Microsoft YaHei;font-size:18px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/testView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="2" android:background="#aa0000" android:gravity="center" android:singleLine="true" android:text="这是第一行" android:textSize="25pt" /> <TextView android:id="@+id/testView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#0000aa" android:gravity="center_vertical|left" android:text="这是第二行" android:textSize="15pt" /></LinearLayout></span>
二、TableLayout的使用方法
(1)重要属性:
1)android:stretchColumns
值:n 拉伸第n列(n从0开始) 使该行填充满
附练习代码:
<span style="font-family:Microsoft YaHei;font-size:18px;"><?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns="0"> <TableRow> <TextView android:background="#aa0000" android:text="1行0列" /> <TextView android:background="#00aa00" android:text="1行1列" /> <TextView android:background="#0000aa" android:text="1行2列" /> </TableRow> <TableRow> <TextView android:background="#0000aa" android:text="2行0列" /> <TextView android:background="#aa0000" android:text="2行1列" /> <TextView android:background="#00aa00" android:text="2行2列" /> </TableRow></TableLayout></span>
第十课:Activity的布局初步(二)
一、多个布局的嵌套
附练习代码:
<span style="font-family:Microsoft YaHei;font-size:18px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="#aa0000" android:gravity="center_horizontal" android:text="RED" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="#00aa00" android:gravity="center_horizontal" android:text="GRE" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="#0000aa" android:gravity="center_horizontal" android:text="BLU" /> <TextView android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" android:background="#aaaa00" android:gravity="center_horizontal" android:text="YEL" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" > <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:stretchColumns="0" > <TableRow> <TextView android:background="#aa0000" android:text="1行0列" /> <TextView android:background="#00aa00" android:text="1行1列" /> <TextView android:background="#0000aa" android:text="1行2列" /> </TableRow> <TableRow> <TextView android:background="#0000aa" android:text="2行0列" /> <TextView android:background="#aa0000" android:text="2行1列" /> <TextView android:background="#00aa00" android:text="2行2列" /> </TableRow> </TableLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#aa0000" android:gravity="center_horizontal" android:text="RED" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#00aa00" android:gravity="center_horizontal" android:text="GRE" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#0000aa" android:gravity="center_horizontal" android:text="BLU" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="#aaaa00" android:gravity="center_horizontal" android:text="YEL" /> </LinearLayout></LinearLayout></span>
第十一课:Activity的布局初步(三)
一、RelativeLayout的使用方法
(1)基本概念
(2)与其他布局的区别
相对布局中的控件的位置依赖于其他控件的位置,位置比较灵活,但是掌控起来比较困难。
(3)常用属性介绍
1)layout_above 将该控件的底部置于给定ID的控件之上
2)layout_below 将该控件的顶部置于给定ID的控件之下
3)layout_toLeftOf 将该控件的右边缘和给定ID的控件的左边缘对齐
4)layout_toRightOf 将该控件的左边缘和给定ID的控件的右边缘对齐
5)layout_alignBaseline 将控件的baseline和给定ID的控件的baseline对齐
6)layout_alignBottom 将控件的底部边缘与给定ID控件的底部边缘对齐
7)layout_alignLeft 将该控件的左边缘与给定ID控件的左边缘对齐
8)layout_alignRight 将该控件的右边缘与给定ID控件的右边缘对齐
9)layout_alignTop 将给定控件的顶部边缘与给定ID控件的顶部对齐
10)layout_alignParentBottom 如果该值为true,则将该控件的底部和父控件的底部对齐
11)layout_alignParentLeft 如果该值为true,则将该控件的左边和父控件的左边对齐
12)layout_alignParentRight 如果该值为true,则将该控件的右边和父控件的右边对齐
13)layout_alignParentTop 如果该值为true,则将该控件的顶部和父控件的顶部对齐
14)layout_centerHorizontal 如果该值为true,该控件将被置于水平方向的中央
15)layout_centerInParent 如果该值为true,该控件将被置于父控件水平方向和垂直方向的中央
16)layout_centerVertical 如果该值为true,该控件将被置于垂直方向的中央
<span style="font-family:Microsoft YaHei;font-size:18px;"><?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="请输入密码" /> <EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/editText1" android:text="取消" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_toLeftOf="@+id/button1" android:text="确定" /></RelativeLayout></span>
- <7.22>新的启航 Android学习笔记(二)
- <7.21>新的启航 Android学习笔记(一)
- <7.23>新的启航 Android学习笔记(三)
- <7.24>新的启航 Android学习笔记(四)
- <7.25>新的启航 Android学习笔记(五)
- 开始新的启航
- 2017.7 新的启航
- Android菜鸟的启航
- 2012,梦想启航(新的开始)
- MATLAB的getstart入门手册学习(启航篇)
- 新Android学习笔记
- android启航
- Android学习笔记(二)创建新Project及结构说明
- JSP新手启航(二)tomcat
- 菜鸟启航:UITextField基本操作(二)
- Android学习笔记(二)
- Android学习笔记(二)
- android学习笔记(二)
- 附注:友链检测限制检测前100条数据
- 使用sencha touch cmd 4.0 创建项目、打包(加入全局变量、公用类、自定义扩展、资源文件)
- 程序员长期保持身心健康的几点建议
- RabbitMQ(1)
- 134_leetcode_LRU Cache
- <7.22>新的启航 Android学习笔记(二)
- 关联容器 map
- crm操作产品实体
- IE6,IE7,IE8,IE9,FIREFOX,Chrome等浏览器兼容详解收集
- 带有名字空间(xmlns)的解析问题
- 长连接 网络断开问题
- [模式识别].(希腊)西奥多里蒂斯<第四版>笔记6之__feature generation (1):数据转换和降维
- Mysql cheat sheet——Mysql使用方便查
- android如何让service不被杀死