android實習程序2


內部類:成員、靜態、匿名、局部(匿名最多)
8種基本類型、引用類型

常見佈局:
線性佈局、表格佈局、幀佈局、相對佈局

幾種常用的佈局
 1、線性佈局(LinearLayout)
  android:gravity設置組件在該佈局中的位置
    bottom:設置組件在佈局的底部
    center_vertical:垂直居中
    bottom|center_horizontal:在佈局的底部,水平居中
 2、表格佈局(TableLayout)
 設置第二列可拉伸,3列可壓縮
  android:stretchColumns="1"
   android:shrinkColumns="2"
     設置第二列隱藏
   android:collapseColumns="1"
 3、幀佈局(FrameLayout)
 4、相對佈局
 5、絕對佈局
 作業:
  1、 掌握自定義組件
  2、掌握三種佈局
  3、寫一個登錄界面

1、線性佈局:<LinearLayout>
2、表格佈局:
<TableLayout >
3、幀佈局:<FramLayout>


1、實現圖片的瀏覽
2、實現小球的拖動
3、實現小球的碰壁折回運動
4、佈局(漸變色)
===============================
2、小球隨觸屏而動
1、.java
package com.tarena.day02_02;

import android.app.Activity;
import android.os.Bundle;
import android.text.Layout;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;

public class Day02_02Activity extends Activity {
MyView myview1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        LinearLayout layout = (LinearLayout)findViewById(R.id.root);
         myview1 = new MyView(this);
         layout.addView(myview1);
       
        myview1.setOnTouchListener(new Ontouch());         
    }
    
    class Ontouch implements OnTouchListener{
    public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
     myview1.x= event.getX();
     myview1.y = event.getY();
     //重繪組件
     myview1.invalidate();
return true;
}
    }
    }


2.java  定義圓點(自定義組件)

package com.tarena.day02_02;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;

//自定義組件
public class MyView extends View{
public float x=50;
public float y=50;

public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
protected void onDraw(Canvas canvas)
{
//畫圓,創建畫筆對象
Paint paint = new Paint();
paint.setColor(Color.RED);
canvas.drawCircle(x,y,15,paint);
}
}

3.main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
     android:id="@+id/root">
</LinearLayout>

實現效果爲可拖動圓點移動
===============================
3、改動.java, 實現小球的折回碰壁運動
.java
package com.tarena.day02_02;


import java.util.logging.LogRecord;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import android.text.Layout;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;

public class Day02_02Activity extends Activity {
MyView myview1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        LinearLayout layout = (LinearLayout)findViewById(R.id.root);
         myview1 = new MyView(this);
         layout.addView(myview1);
         
         
        final Handler h= new Handler(){

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
if(msg.what == 0x1122){
         myview1.invalidate();
         }
}
 
         };
         
        //匿名內部類
         new Thread(){
          int a=5,b=5;
         public void run(){
        
         while(true){
         int width = myview1.getWidth();
         int height = myview1.getHeight();    
        
         if(myview1.x>= width - 15) a=-a;
         if(myview1.x<=0+15) a=-a;
         myview1.x+=a;
        
        
         if(myview1.y>=height-15)b=-b;
         if(myview1.y<=0+15) b=-b;
         myview1.y-=b;
         try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
        
         Message msg =  new Message();
         msg.what=0x1122;
         h.sendMessage(msg);
        
        
         }    
         }
         }.start();
         
  
        //myview1.setOnTouchListener(new Ontouch());         
    }
    
    class Ontouch implements OnTouchListener{
    public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
     myview1.x= event.getX();
     myview1.y = event.getY();
     //重繪組件
     myview1.invalidate();
return true;
}
    }
    }

===================================
1、圖片的瀏覽(點擊按鈕,實現循環翻頁)
.java

package com.tarena.day02;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class Day02Activity extends Activity {
//靜態初始化
//int imgs[]; C++的寫法
int[] imgs ={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d};
int curInt = 0;
ImageView img,imgleft,imgright;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
      Button buttonleft = (Button )findViewById(R.id.button1);
    buttonleft.setOnClickListener(new Clickleft());
    
    Button buttonright = (Button )findViewById(R.id.button2);
    buttonright.setOnClickListener(new Clickright());  
    
    LinearLayout layout =(LinearLayout)findViewById(R.id.root);
    //在layout上添加組件
    img= new ImageView(this);
    layout.addView(img);
    img.setImageResource(R.drawable.a);
     
    /*imgleft= new ImageView(this);
    layout.addView(imgleft);
    imgleft.setImageResource(R.drawable.left);   
    imgleft.setOnClickListener(new Clickleft()); 
    
    imgright= new ImageView(this);
    layout.addView(imgright);
    imgright.setImageResource(R.drawable.right);   
    imgright.setOnClickListener(new Clickleft()); */   
    
    }
   
      class Clickleft implements OnClickListener{
    
public void onClick(View v) {
// TODO Auto-generated method stub
img.setImageResource(imgs[curInt%4]);
curInt--;
}
    }
      
      class Clickright implements OnClickListener{
         
     public void onClick(View v) {
     // TODO Auto-generated method stub
     img.setImageResource(imgs[curInt%4]);
     curInt++;
    
    
     }
         }
      
    } 
 -----------------------------------------------------------------
也可實現點擊圖片,進行翻頁  




====================
 4、佈局
1\漸變色
.main

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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:width="210px"
        android:height="60px"
        android:background="#ff0000"
        android:id="@+id/t1"
        /> 
        <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="180px"
        android:height="60px"
        android:background="#ee0000"
        android:id="@+id/t2"
        /> 
        <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="150px"
        android:height="60px"
        android:background="#dd0000"
        android:id="@+id/t3"
        /> 
        <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="120px"
        android:height="60px"
        android:background="#990000"
        android:id="@+id/t4"
        /> 
        <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="90px"
        android:height="60px"
        android:background="#770000"
        android:id="@+id/t5"
        /><TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="60px"
        android:height="60px"
        android:background="#550000"
        android:id="@+id/t6"
        />  
        <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="30px"
        android:height="60px"
        android:background="#330000"
        android:id="@+id/t7"
        /> 
</FrameLayout>
 
---
.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<color name="color7">#330000</color>
<color name="color6">#550000</color>
<color name="color5">#770000</color>
<color name="color4">#990000</color>
<color name="color3">#dd0000</color>
<color name="color2">#ee0000</color>
<color name="color1">#ff0000</color>
</resources>

-----

.java

package com.tarena.day0203;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class Day02_03Activity extends Activity {
   int[] colors = {R.color.color7,R.color.color6,R.color.color5,R.color.color4,R.color.color3,R.color.color2,R.color.color1};
   int curInt=0;
   int[] tId = {R.id.t1,R.id.t2,R.id.t3,R.id.t4,R.id.t5,R.id.t6,R.id.t7};
 TextView[] ts = new TextView[7];
   /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
       for(int i=0;i<7;i++){
        ts[i]=(TextView)findViewById(tId[i]);
       }
       final Handler h = new Handler(){
      @Override
     public void handleMessage(Message msg) {
      for(int i=0;i<7-curInt;i++){
       ts[i].setBackgroundResource(colors[i+curInt]);
      }
     } 
       };
       new Thread(){
        public void run() {
         while(true){
         curInt++;
         if(curInt>=7){
          curInt=0;
         }
         Message m = new Message();
         m.what=0x1122;
         h.sendMessage(m);
         try {
    Thread.sleep(100);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
         }
        }
       
       }.start();
    }
}

 

 

實現效果爲:
 

2、格式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
     >
<TableLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1"
    android:shrinkColumns="2"
    >
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="單獨佔一行"
        />
    <TableRow >
         <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="普通按鈕"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="可拉伸的按鈕按鈕"
            />
         <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="可壓縮的按鈕按鈕"
            />
        
    </TableRow>
    
</TableLayout>
<TableLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1,2"
    >
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="單獨佔一行"
        />
    <TableRow>
         <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="普通按鈕"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="可拉伸的按鈕按鈕"
            />
         <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="可壓縮的按鈕按鈕"
            />
        
    </TableRow>
</TableLayout>
<TableLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:collapseColumns="1"
    >
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="單獨佔一行"
        />
    <TableRow>
         <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕一"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="可按鈕二"
            />
         <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕三"
            />
        
    </TableRow>
</TableLayout>
<TableLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="單獨佔一行"
        />
    <TableRow>
         <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕一"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="可按鈕二"
            />
         <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕三"
            />
        
    </TableRow>
     <TableRow>
         <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按鈕一"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="可按鈕二"
            />
    </TableRow>
</TableLayout>
</LinearLayout>
實現效果爲:

  
    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章