14 thg 5, 2011

Game Sudoku (Android) -- Implementing an About Box

Mục đích của bài này là khi bạn nhấp vào Button About trong màn hình của bài trước. bạn sẽ show ra thông tin về cần thiết về game của bạn.
2 vấn đề chính :
    1. Định nghĩa 1 Activity mới và khởi động nó.
    2. Sử dụng lớp AlertDialog và hiển thị nó lên.
Thực hiện :

Bạn vào res/layout tạo 1 file xml About.xml như sau :

  1. <?xml version="1.0" encoding="utf-8"?>
    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="@color/backgound"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dip" >
        <TextView
            android:id="@+id/about_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/about_text" />
    </ScrollView>

Sau đó bạn vào file strings.xml thêm vào nội dung sau :

  1. <string name="about_title">About Android Sudoku</string>
        <string name="about_text">Sudoku dc phat trien boi Sungha-CN08A-GTVT</string>

Vậy là bạn đã tạo xong nội dung và layout cho About.
Bây giờ bạn vào tạo 1 class About.java bạn viết code như sau :

  1. package org.example.Sudoku;

    import android.app.Activity;

    import android.os.Bundle;

    public class About extends Activity{

        @Override
        protected void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.about);
        }
    }


 Trong class Sudoku.java bạn viết code như sau :

  1. package org.example.Sudoku;

    import android.app.Activity;

    import android.app.AlertDialog;
    import android.os.Bundle;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    public class Sudoku extends Activity implements OnClickListener {
        /** Called when the activity is first created. */
        private static final String TAG = "Sudoku" ;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
           
            //Set up clicks listener all button
            View continueButton = findViewById(R.id.continue_button);
                 continueButton.setOnClickListener(this);
            View newButton = findViewById(R.id.new_button);
                 newButton.setOnClickListener(this);
            View aboutButton = findViewById(R.id.about_button);
                 aboutButton.setOnClickListener(this);
            View exitButton = findViewById(R.id.exit_button);
                 exitButton.setOnClickListener(this);
           }
      ///////////////////////////////////////
  1.   public void onClick(View v) {
               switch (v.getId()) {
               case R.id.about_button:
              Intent i = new Intent(this, About.class);
              startActivity(i);
              break;
                 // More buttons go here (if any) ...
    }
    }

 Trong AndroidManifest.xml bạn đăng kí bằng dòng lệnh sau :

  1. <activity android:name=".About"
               android:label="@string/about_title" >
    </activity>

Tạo Theme cho layout :

Vào trang AndroidManifest.xml them đoạn code sau :

  1. <activity android:name=".About"
              android:label="@string/about_title"
              android:theme="@android:style/Theme.Dialog" >
    </activity>


Làm đúng các bước trên các bạn có kết quả sau :

    10 thg 5, 2011

    Display Video thumbnail in ListView

    In this exercise, the path of video sources are hard-coded in a String[]. A ListView with thumbnail, and path of the videos will be implemented.
     remark: For minimum API Level 8, Android 2.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="wrap_content"
    android:orientation="horizontal">
    <ImageView
    android:id="@+id/Thumbnail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon"/>
    <TextView
    android:id="@+id/FilePath"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
    </LinearLayout>


    AndroidThumbnailList.java

    package com.exercise.AndroidThumbnailList;
    
    import android.app.ListActivity;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.media.ThumbnailUtils;
    import android.os.Bundle;
    import android.provider.MediaStore.Video.Thumbnails;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    public class AndroidThumbnailList extends ListActivity{
    
    String[] videoFileList = {
      "/sdcard/Video/Android 2.0 Official Video_low.mp4",
      "/sdcard/Video/Android 2.2 Official Video_low.mp4",
      "/sdcard/Video/Android 2.3 Official Video_low.mp4",
      "/sdcard/Video/Android 3.0 Preview_low.mp4",
      "/sdcard/Video/Android Demo_low.mp4",
      "/sdcard/Video/Android in Spaaaaaace!.mp4",
      "/sdcard/Video/Android in Spaaaaaace!_low.mp4",
      "/sdcard/Video/What is an Android phone-_low.mp4"
    };
    
    public class MyThumbnaildapter extends ArrayAdapter<String>{
    
     public MyThumbnaildapter(Context context, int textViewResourceId,
       String[] objects) {
      super(context, textViewResourceId, objects);
      // TODO Auto-generated constructor stub
     }
    
     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
     
      View row = convertView;
      if(row==null){
       LayoutInflater inflater=getLayoutInflater();
       row=inflater.inflate(R.layout.row, parent, false);
      }
     
      TextView textfilePath = (TextView)row.findViewById(R.id.FilePath);
      textfilePath.setText(videoFileList[position]);
      ImageView imageThumbnail = (ImageView)row.findViewById(R.id.Thumbnail);
     
      Bitmap bmThumbnail;
            bmThumbnail = ThumbnailUtils.createVideoThumbnail(videoFileList[position], Thumbnails.MICRO_KIND);
            imageThumbnail.setImageBitmap(bmThumbnail);
     
      return row;
     }
    
    }
    
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setListAdapter(new MyThumbnaildapter(AndroidThumbnailList.this, R.layout.row, videoFileList));
      }
    }

    8 thg 5, 2011

    Game Sudoku (Android) -- Tạo Layout cho game

    Hnay mình sẽ giới thiệu cho các bạn cách tạo layout cho game sudoku này.
    Sau khi hoàn thành chúng ta sẽ có dc kết quả sau :


    Trước hết bạn phải hình dung ra rằng game của mình sẽ có layout như thế nào.

    ở đây mình tạo layout theo dạng Linear Layout. Các bạn có thể chọn dạng Layout khác tùy ý. Các bạn có thể xem 1 số bài mình đã hướng dẫn tạo layout như :
    Trước hết bạn vào trong res/values/colors.xml để tạo background cho game của bạn :
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
          <color name="backgound">#3500ffff</color>
    </resources>
    Và trong file res/values/strings.xml bạn viết code như sau :
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="app_name">Sudoku</string>
        <string name="main_title">Android Sudoku</string>
        <string name="continue_lable">Continue</string>
          <string name="new_game_lable">New Game</string>
          <string name="about_lable">About</string>
          <string name="exit_lable">Exit</string>
    </resources>
    Đến đây bạn đã tạo dc màu background cho ứng dụng của bạn và các lable cho các button của bạn sẽ làm sau đây.
    Tiếp theo bạn tạo layout chính cho chương trình :
    Trong ví dụ của mình, mình chia làm 2 Linear layout.1 cái chứa image logo game của ứng dụng, cái còn lại chức các button.
    Bạn vào file layout/main.xml
    <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1">
                <ImageView
                      android:id="@+id/imgSudoku"
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:src="@drawable/image"
                       />
          </LinearLayout>  
    Logo : ten image của bạn.
    Để vẽ các button bạn tạo ra 1 LinearLayout như sau :
    <LinearLayout
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="center">   
          <Button
                android:id="@+id/continue_button"
                android:text="@string/continue_lable"
                android:layout_width="200px"
                android:layout_height="50px"
                android:lines="1"
                android:layout_gravity="center"
                android:padding="10px"/>
          <Button
                android:id="@+id/new_button"
                android:text="@string/new_game_lable"
                android:layout_width="200px"
                android:layout_height="50px"
                android:lines="1"
                android:layout_gravity="center"
                android:padding="10px"
                />
          <Button
                android:id="@+id/about_button"
                android:text="@string/about_lable"
                android:layout_width="200px"
                android:layout_height="50px"
                android:lines="1"
                android:layout_gravity="center"
                android:padding="10px"
                />
          <Button
                android:text="@string/exit_lable"
                android:id="@+id/exit_button"
                android:layout_width="200px"
                android:layout_height="50px"
                android:lines="1"
                android:layout_gravity="center"
                android:padding="10px"
                />
                <!-- <TextView
                android:text="@string/main_title"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_marginBottom="25dip"
                android:textSize="24.5sp" /> -->
          </LinearLayout>  
    Các bạn có thể tùy biến các kiểu layout khác nhau cho màn hình game của mình.






    Quản Lý Học Sinh

    Xây dựng chức năng nhập xuất thông tin cá nhân và kết quả học tập của một học sinh.
    Trong đó:
    Thông tin học sinh gồm: họ tên, ngày sinh
    Kết quả học tập gồm: tên môn học, điểm học kỳ 1, điểm học kỳ 2, điểm cả năm
    Yêu cầu: nhập thông tin và kết quả học tập của một học sinh và xuất kết quả
    Điểm cả năm của từng môn học được tính theo công thức:
    Điểm cả năm = (Điểm HKI + Điểm HKII * 2 ) / 3
    Điểm trung bình được tính theo công thức:
    Điểm trung bình = tổng điểm cả năm của tất cả môn học / số môn học

    import java.io.*;
    
    public class QLHS {
        private String HoTen;
        private String NgaySinh;
        //danh sach bang diem mon hoc cua hoc sinh
        private Diem []BangDiem;
    
    
        public QLHS()throws Exception {
         nhapThongTin();
         XuatThongTin();
        }
    
        public void nhapThongTin() throws Exception{
            System.out.print("Ho Ten: ");
            BufferedReader _nhap=new BufferedReader(new InputStreamReader(System.in));
            HoTen=_nhap.readLine();
            System.out.print("Ngay Sinh: ");
            NgaySinh=_nhap.readLine();
            System.out.print("So Luong Mon Hoc: "); //Số lượng môn học phải nằm ở lớp Điểm chứ không thể ở đây
            int n= Integer.parseInt(_nhap.readLine());
            BangDiem= new Diem[n];
            //nhap diem cua tung mon hoc
            for(int i=0;i<n;++i){
             BangDiem[i]=new Diem();
             BangDiem[i].nhapDiem();
             System.out.println("");
            }
        }
    
        public void XuatThongTin(){
         System.out.println("Ho Ten: "+HoTen);
         System.out.println("Ngay Sinh: "+NgaySinh);
         System.out.println("STT    Mon          Diem HK1     Diem HK2     Diem TB");
         for (int i = 0; i<BangDiem.length; i++){
          BangDiem[i].xuatDiem(i+1);
         };
         System.out.printf("Diem Trung Binh: %2.2f\n",DiemTBCaNam());
         System.out.println("Xep Loai: "+XepLoai());
        }
    
        public String XepLoai(){
         if(DiemTBCaNam()<5.0)
          return "yeu";
         else if(DiemTBCaNam()>=5.0 && DiemTBCaNam()<6.5)
          return"Trung Binh";
         else if(DiemTBCaNam()>=6.5 &&DiemTBCaNam()<7.5)
          return"Kha";
         else if(DiemTBCaNam()>=7.5 && DiemTBCaNam()<8.5)
         return"Xuat Sac";
        }
    
        public double DiemTBCaNam(){
         double tong=0;
         for (int i = 0; i<BangDiem.length; i++){
          tong+=BangDiem[i].DiemTrungBinh();
         };
         return tong/BangDiem.length;
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args)throws Exception {
            QLHS temp= new QLHS();
        }
    }
    //Xay Dung Class Diem De quang ly bang diem cua tung hoc sinh
    class Diem{
     private String TenMon;
     private double DiemHK1;
     private double DiemHK2;
    
     public Diem(String _ten, double _DiemHK1,double _DiemHK2){
      TenMon=_ten;
      DiemHK1=_DiemHK1;
      DiemHK2=_DiemHK2;
     }
     public Diem(){
    
     }
    
     public void nhapDiem() throws Exception{
            System.out.print("\tTen Mon: ");
            BufferedReader _nhap=new BufferedReader(new InputStreamReader(System.in));
            TenMon=_nhap.readLine();
            System.out.print("Diem Hoc Ki 1: ");
            DiemHK1=Double.parseDouble(_nhap.readLine());
            System.out.print("Diem Hoc Ki 2: ");
            DiemHK2=Double.parseDouble(_nhap.readLine());
    
     }
     public void xuatDiem(int i){
      //in theo dang Stt tenMon DiemHK1 DiemHK2 DiemTB
      System.out.printf("%3d    %-12s %-12.2f %-12.2f %2.2f \n",i,TenMon,DiemHK1,DiemHK2,DiemTrungBinh());
            //System.out.println(i+"    "+TenMon+"         "+DiemHK1+"         "+DiemHK2+"    "+DiemTrungBinh());
        }
    
     public double DiemTrungBinh(){
      return (DiemHK1+DiemHK2*2)/3;
     }
    }

    Bản beta đầu tiên

    Sau 6 tháng cả team cặm cụi làm việc điên cuồng, bản alpha cũng được giới thiệu ra toàn bộ công ty và được testing nội bộ công ty mà thôi. ...