安卓课程小结
暑假集训完,我们又学了一点关于安卓手机开发的知识,尽管只有几节课,但是收益匪浅啊。
这相当于我们又接触了一门语言啊,俗话说的好师傅领进门,修行在个人啊。
现在就稍微聊聊android开发,
其实我们是以一些小游戏切入来学习android的,有小游戏展开,再讲一些其他的。

首先布局格式
java中的布局靠窗体,面板等来嵌套然后在一个个添加组件,而安卓中则是在xml(res/layout/***.xml)文件中书写组件也在其中按一定顺序写
其格式如下

<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:text="用户登录"        android:textSize="30px"        tools:context=".MainActivity" />    <!-- 嵌套布局 -->    <LinearLayout        xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_horizontal"            android:text="账号"            android:textSize="20px" />        <EditText            android:id="@+id/edit_name"            android:layout_width="100px"            android:layout_height="wrap_content" />    </LinearLayout>    <LinearLayout        xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_horizontal"            android:text="密码"            android:textSize="20px" />        <EditText            android:id="@+id/edit_pwd"            android:layout_width="100px"            android:layout_height="wrap_content" />    </LinearLayout>    <!-- 按钮对象 -->    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"         android:orientation="horizontal"        xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_gravity="center_horizontal"        >        <Button android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="登录"            android:layout_gravity="center_horizontal"            android:id="@+id/btn_login"/>        <Button android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="重置"            android:layout_gravity="center_horizontal"            android:id="@+id/btn_reset"/>    </LinearLayout></LinearLayout>


然后是监听器
监听器也有不同:
java中的监听器addListener(),而后者则是setListener()。
而且,java中的监听器要加在需要监听的地方,而后者则有时不用例如:画图板
不过想一些按钮类的监听器是需要在其上加监听器
注:画图板与java中的不太一样,请看如下代码示例
public class MyView extends ImageView {       //位图对象Bitmap map;        //画布对象Canvas c;       //画笔对象Paint paint;float x1, y1, x2, y2;public MyView(Context context, AttributeSet attrs) {super(context, attrs);/** * 实例化对象 */map = Bitmap.createBitmap(300, 400, Config.ARGB_8888);c = new Canvas(map);paint = new Paint();}public boolean onTouchEvent(MotionEvent me) {// 获取坐标int action = me.getAction();switch (action) {case MotionEvent.ACTION_DOWN:x1 = me.getX();y1 = me.getY();break;case MotionEvent.ACTION_UP:x2 = me.getX();y2 = me.getY();invalidate();break;}return true;}protected void onDraw(Canvas canvas) {super.onDraw(canvas);                canvas.drawLine(x1,y1, x2), y2, paint);canvas.drawBitmap(map, 0, 0, null);}}

注:监听器一般写在onCreat()方法所在的类中。
public class main extends Activity {public static String tool = "Line";@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);getTool();}/** * 监听器 */ OnClickListener listener = new OnClickListener(){@Overridepublic void onClick(View v) {int id = v.getId();switch(id){case R.id.black:color=Color.BLACK;break;case R.id.green:color=Color.GREEN;break;case R.id.white:color=Color.WHITE;break;}}  } ;/** *给工具按钮添加监听器 */public void getTool() {Button Oval = (Button) findViewById(R.id.Oval);Button Line = (Button) findViewById(R.id.Line);Button Rect = (Button) findViewById(R.id.Rect);Oval.setOnClickListener(listener1);Line.setOnClickListener(listener1);Rect.setOnClickListener(listener1);}}

//监听触摸即类似于鼠标监听器
public boolean onTouchEvent(MotionEvent me) {// 获取坐标int action = me.getAction();switch (action) {case MotionEvent.ACTION_DOWN:x1 = me.getX();y1 = me.getY();break;case MotionEvent.ACTION_UP:x2 = me.getX();y2 = me.getY();if (main.tool.equals("Line")) {shape = new ShapeLine(x1, x2, y1, y2, paint, 5, main.color);mq.add(shape);}if(main.tool.equals("Rect")){shape = new ShapeRect(x1,x2,y1,y2,paint,1,main.color);mq.add(shape);}if(main.tool.equals("Oval")){shape = new ShapeOval(x1,x2,y1,y2,paint,1,main.color);mq.add(shape);}invalidate();break;}return true;}


最后是主函数:
java中的main函数可看做Activity中的onCreat方法
Activity中有六个个方法
public class Activity extends ApplicationContext {
protected void onCreate(Bundle savedInstanceState);

protected void onStart();

protected void onRestart();

protected void onResume();

protected void onPause();

protected void onStop();

protected void onDestroy();
}
必备(重写)onCreat()方法。其他根据情况选用(重写)。
重写onCreat方法
 @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //获取按钮对象        bu_add = (Button)findViewById(R.id.addBall);        bu_pause = (Button)findViewById(R.id.pause);        bu_remove=(Button)findViewById(R.id.remove);        bu_add.setOnClickListener(click);        bu_pause.setOnClickListener(click);        bu_remove.setOnClickListener(click);           }

重写哦那Destroy()方法
 protected void onDestroy() {    super.onDestroy();    list.clear();    };

注:android中的界面跳转一定要在*.xml(在项目的根目录下)文件中注册
其实吧,我觉得总体来说我们还是用java语言在写,虽然有些方法不太一样,尤其是在xml文件中

更多相关文章

  1. 《第一行代码》学习笔记之服务
  2. Android中Handler,Looper,MessageQueue和Thread
  3. 使用多字节字符集的跨平台(PC、Android、IOS、WP)编码/解码方法
  4. Android(安卓)Hybird App开发
  5. Android基于监听的事件处理
  6. Android(安卓)中使用 dlib+opencv 实现动态人脸检测
  7. Back Stack学习之Android退出方法小结
  8. Android内存泄漏检测及修复(转载)
  9. Android消息机制之HandlerThread

随机推荐

  1. 从Android(安卓)8.0源码的角度剖析Androi
  2. Android如何加载大图,防止OOM
  3. Android访问WCF服务(上篇)-服务端开发
  4. 浅谈Android五大布局(一)――LinearLayout
  5. Debug Android(安卓)and Linux suspend a
  6. Android(安卓)NullPointerException解决
  7. Android重要类学习之——Activity
  8. 标题:[资讯]注意!Android惊爆两安全漏洞
  9. Android(安卓)根文件系统启动过程(init进
  10. 在Ubuntu16.04上下载并编译Android内核源