Android(安卓)初学入门代码注释 学习笔记001 16.03.18
package com.text.helloworld;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import android.view.*;
import android.view.View.OnClickListener;
/*所有活动需在AndroidManifest.xml中注册 才能生效
* 在AndroidManifest.xml中看下面的
* 活动注册需放到<application 中通过<activity 来注册
* a、 首先 android:name指定注册哪个 .helloworld等同于 包名.helloworld 最外面manifest标签已指定包名
* b、 android:label指定活动中标题栏内容 给主活动指定的还会成为启动器(Launcher)中应用程序显示的名称
* c、 中声明的是主活动和首先打开这个活动>>*/
public class HelloworldActivity extends Activity {
Button button_1,button_2,button_3,button_4;
Button table11,table12,table21,table22;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*隐藏标题栏 在setContentView()之前写 不然报错*/
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
/*根据Id找到界面中的两个按钮组件*/
button_1=(Button)this.findViewById(R.id.button_1);
/*findView返回的是View对象 向下转型为Button对象
*/ button_2=(Button)this.findViewById(R.id.button_2);
button_3=(Button)this.findViewById(R.id.button_3);
button_4=(Button)this.findViewById(R.id.button_4);
table11=(Button)this.findViewById(R.id.table11);
/*一、匿名方式处理事件*/
table11.setOnClickListener(new OnClickListener(){
public void onClick(View v){
/*隐式Intent 启动活动
* 1、在AndroidManifest.xml注册时额外加标签
* 2、标签指明当前活动可以响应 包名.ACTION_START这个action
* 3、标签包含附加信息 精确指明当前活动能够响应的Intent中还可能带有category
* 4、只有和中内容同时能够匹配上
* Intent中指定的action和category才响应
* 每个Intent只能指定一个action 却多个category*/
Intent intent =new Intent("com.text.helloworld.ACTION_START");
startActivity(intent); } });
/*二、显式处理事件*/
/*创建监听器对象*/
ColorListener cl=new ColorListener();
/*注册监听*/
button_1.setOnClickListener(cl);
button_2.setOnClickListener(cl);
button_3.setOnClickListener(cl);
button_4.setOnClickListener(cl); }
/*创建按钮监听器,继承OnClickListener监听接口*/
class ColorListener implements OnClickListener{ /*实现单击事件处理方法*/
public void onClick(View v){
if(v==button_1){
getWindow().setBackgroundDrawableResource(R.color.yellow);
/*使用toast 显示提醒
* 1、使用静态方法maketext创建Toast对象 调用show()将其显示
* 2、maketext方法传入三个参数
* a、context 上下文 因为活动本身是context对象 直接传入HelloActivity.this
* b、text 显示的文本内容
* c、 Toast 显示的时长 有俩:
Toast.LENGTH_SHORT和Toast.LENGTH_LONG * */
Toast.makeText(HelloworldActivity.this, "you clicked button 1 and change yellow", Toast.LENGTH_SHORT).show(); }
if(v==button_2){ getWindow().setBackgroundDrawableResource(R.color.blue);
Toast.makeText(HelloworldActivity.this, "you clicked button 2 and change blue", Toast.LENGTH_SHORT).show(); }
if(v==button_3){
/*销毁一个活动 与back键功能相同*/
finish(); }
if(v==button_4){
/* 显式Intent 启动活动 Intent(Context packageContext,Class<?>cls) 接收两个参数 1、Context要求的活动上下文 2、Class则是指定要想启动的目标活动 ->构造出意图 使用Intent: 使用Activity提供的startActivity()方法 此方法专用来启动活动的 接收一个Intent参数 这里将我们构建的Intent传入startActivity() 就可以启动活动了*/
Intent intent =new Intent(HelloworldActivity.this,SecondActivity.class); startActivity(intent); } } }
/*创建菜单 将其显示
* 重写onCreateOptionsMenu()方法
* 通过getMenuInflater()方法得到MenuInflater对象 调用inflate()当前活动创建菜单
* inflate()方法接收俩参数
* a、指定通过哪个资源文件来创建菜单
* b、指定菜单项添加到哪个Menu对象中*/
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.memain, menu); return true;
/*true 允许创建的菜单显示出来 返回false 为不显示 */ }
/*定义菜单响应事件
* 重写onOptionsItemSelected()方法
* 调用item.getItemId()方法判断点击的哪个菜单
* 然后加入每个菜单的逻辑处理 这里弹出一个Toast*/
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.add_item: Toast.makeText(this, "you clicked add", Toast.LENGTH_SHORT).show();
break;
case R.id.remove_item: Toast.makeText(this, "you click Remove",
Toast.LENGTH_SHORT).show(); break; default: }
return true; }}
更多相关文章
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用
- python list.sort()根据多个关键字排序的方法实现
- android图片压缩并转为base64字符串
- Android退出多个Activity的两个经典方法
- Android(安卓)Wifi的一些属性
- Android保持屏幕常亮
- Android学习笔记(9)————Android的三种Menu用法
- Android读取本地TXT文件