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; }}



更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. Python list sort方法的具体使用
  3. python list.sort()根据多个关键字排序的方法实现
  4. android图片压缩并转为base64字符串
  5. Android退出多个Activity的两个经典方法
  6. Android(安卓)Wifi的一些属性
  7. Android保持屏幕常亮
  8. Android学习笔记(9)————Android的三种Menu用法
  9. Android读取本地TXT文件

随机推荐

  1. android 使控件透明
  2. Android(安卓)存储选项之 ContentProvide
  3. android定位布局
  4. Android(安卓)Camera 使用小结
  5. android layout_weight了解
  6. android自定义view属性
  7. Android修改自己程序字体的方法详解
  8. 由Android想到的事情
  9. Android(安卓)常用组件,的常用类型
  10. Android(安卓)Studio中使用android:src="