代码改变世界

作业2

2017-03-27 19:09  好名字啊  阅读(134)  评论(6)    收藏  举报

教材学习内容

  1. android开发环境的搭建
  2. 基本控件的建立(TextView.Buttun.EditText.RadioGroup等)
  3. 触屏与键盘事件(onTouchEvent事件)
  4. 菜单与消息通知

闪退事件与空指针事件处理

闪退
  1. 定义变量
  2. 建立相关相应事件
空指针
  1. 重启电脑
  2. clean rebuild
  3. file下面的Invalidate caches
  4. 查找代码错误

常用界面控件

序号 属性名称 作用描述
1 TextView 显示文本信息,用于向用户显示文本内容,但不允许编辑
2 Butten 普通按钮,控件单击,引发相应事件的处理函数
3 EditText 可编辑文本框主键,数据传送
4 ImageView 用于显示图片,普通静态图与动态图
5 ImageButten 图片按钮
6 CheckBox 复选框,可一个也可以多个
7 RadioGroup 单选按钮组
8 Spinner 下拉列表组件
9 ProgressBar 进度条
10 SeekBar 拖动条
11 RatingBar 评分组件
12 ListView 列表
13 Dialong 对话框
14 Toast 信息提示组件

例:


 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textSize="20sp"
    android:text="  " />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@mipmap/ic_launcher"/>

<RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/x">
        <RadioButton
            android:id="@+id/xx"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" "
            android:textSize="20sp" />
        <RadioButton
            android:id="@+id/xxx"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" "
            android:textSize="20sp"/>
    </RadioGroup>

<CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/x"
        android:text="  "
        android:textSize="20sp" />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/xx"
        android:text="  "
        android:textSize="20sp" />
    <CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/xxx"
    android:text="  "
    android:textSize="20sp" />
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/xxxx"
        android:text="  "
        android:textSize="20sp" />

onTouchEvent事件

常用方法:

1. MotionEvent.getAction()

屏幕按下时,MotionEvent.getAction()值为MotionEvent.ACTION_DOWN

屏幕抬起时,MotionEvent.getAction()值为MotionEvent.ACTION_UP

屏幕滑动时,MotionEvent.getAction()值为MotionEvent.ACTION_MOVE

2.MotionEvent.getX()

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction()==MotionEvent.ACTION_DOWN){
        float x=event.getX();
        float y=event.getY();
        String pos ="x坐标:"+x+",y坐标:"+y;
        Toast.makeText(this,pos,Toast.LENGTH_LONG).show();
    }
    return true;
}

键盘事件

键盘事件主要用于对键盘事件的监听,根据用户输入内容对键盘事件进行跟踪。控件使用View.OnKeyListener借口进行事件处理


public static interface View.OnKeyListene{
public boolean onKey(View v,int keyCode,KeyEvent event){
  }
}
菜单与消息通知

布局加载菜单

注册事件

上下文菜单

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   menu.add(Menu.NONE,Menu.FIRST+1,1,"保存").setIcon(android.R.drawable.ic_menu_save);
    menu.add(Menu.NONE,Menu.FIRST+2,3,"删除").setIcon(android.R.drawable.ic_menu_delete);
    menu.add(Menu.NONE,Menu.FIRST+3,2,"打开").setIcon(android.R.drawable.ic_menu_add);
    SubMenu subMenu=menu.addSubMenu("子菜单");
    subMenu.add(0,101,0,"子菜单1");
    subMenu.add(0,102,0,"子菜单2");
    getMenuInflater().inflate(R.menu.main,menu);
    MenuInflater inflater=getMenuInflater();
    inflater.inflate(R.menu.main,menu);
    return true;
}

注册

@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.mune,menu);
return true;
}

创建对话框的步骤如下:

  1. 创建一个builder对象。
  2. 设置要创建的dialog的参数,如几个按钮、显示什么内容等。
  3. 为按钮设置回调函数(因为android中的dialog都是异步的,所以需要回调函数)。
  4. 根据上上面几步的设置,使builder生成dialog对象。
    使用show()方法将dialog显示出来

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case Menu.FIRST+1:
        case Menu.FIRST+2:
        case Menu.FIRST+3:
            Toast.makeText(this,item.getTitle().toString()+"菜单被单击了",Toast.LENGTH_LONG).show();
            break;
        case R.id.item_about:
            AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("关于");
            builder.setIcon(R.mipmap.ic_launcher);
            builder.setMessage("版本号为1.0");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    MainActivity.this.finish();

                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            builder.setNegativeButton("退出", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            builder.create().show();
         break;
    }
    return super.onOptionsItemSelected(item);
}