安卓课程小结
暑假集训完,我们又学了一点关于安卓手机开发的知识,尽管只有几节课,但是收益匪浅啊。
这相当于我们又接触了一门语言啊,俗话说的好师傅领进门,修行在个人啊。
现在就稍微聊聊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";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getTool();
}
/**
* 监听器
*/
OnClickListener listener = new OnClickListener(){
@Override
public 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文件中