2014年11月20日 星期四

Android SQLite db檔案匯入與基本指令 SQLite data copy to app and instruction

假設已經有一個SQLite檔,要將其匯入app
1.首先在專案res資料夾建立一個raw資料夾,將SQLite檔案放入此資料夾
2.接著建立一個DBManager.java檔案


public class DBManager {

 private final int BUFFER_SIZE = 400000;
 public static final String DB_NAME = "db2.db"; // 保存的資料庫檔案名
 public static final String PACKAGE_NAME = "tw.android";
 public static final String DB_PATH = "/data"
   + Environment.getDataDirectory().getAbsolutePath() + "/"
   + PACKAGE_NAME;// 在手機裡存放資料庫的位置(/data/data/tw.android/db2.db)

 private SQLiteDatabase database;
 private Context context;

 public DBManager(Context coNtext) {
  this.context = coNtext;
 }

 public SQLiteDatabase getDatabase() {
  return database;
 }

 public void setDatabase(SQLiteDatabase database) {
  this.database = database;
 }

 public void openDatabase() {
  this.database = this.openDatabase(DB_PATH + "/" + DB_NAME);
 }

 private SQLiteDatabase openDatabase(String dbfile) {

  try {
   if (!(new File(dbfile).exists())) {
    Log.i("have db2????????", "no");
    // 判斷資料庫檔案是否存在,若不存在則執行導入,否則直接打開資料庫
    InputStream is = this.context.getResources().openRawResource(
      R.raw.db2); // 欲導入的資料庫
    FileOutputStream fos = new FileOutputStream(dbfile);
    byte[] buffer = new byte[BUFFER_SIZE];
    int count = 0;
    while ((count = is.read(buffer)) > 0) {
     fos.write(buffer, 0, count);
    }
    fos.close();
    is.close();
   }
   SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
     null);
   return db;

  } catch (FileNotFoundException e) {
   Log.e("Database", "File not found");
   e.printStackTrace();
  } catch (IOException e) {
   Log.e("Database", "IO exception");
   e.printStackTrace();
  }
  return null;
 }

 public void closeDatabase() {
  this.database.close();

 }
}
3.主要檔案Main.java搭配基本SQLite指令
public class Main extends Activity {

 private SQLiteDatabase db;

 private EditText mEdtName, mEdtAddr, mEdtUpdate1, mEdtUpdate2, mEdtDelete,
   mEdtSQL;

 private TextView mTextList1, mTextList2;

 private Button mBtnAdd, mBtnUpdate, mBtnDelete, mBtnSQL;

 public DBManager dbHelper;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  setupViewComponent();

  dbHelper = new DBManager(this);
  dbHelper.openDatabase();
  db = dbHelper.getDatabase();
  queryList();
 }

 private void setupViewComponent() {
  mTextList1 = (TextView) findViewById(R.id.textList1);
  mTextList2 = (TextView) findViewById(R.id.textList2);

  mEdtName = (EditText) findViewById(R.id.edtName);
  mEdtAddr = (EditText) findViewById(R.id.edtAddr);
  mEdtUpdate1 = (EditText) findViewById(R.id.edtUpdate1);
  mEdtUpdate2 = (EditText) findViewById(R.id.edtUpdate2);
  mEdtDelete = (EditText) findViewById(R.id.edtDelete);
  mEdtSQL = (EditText) findViewById(R.id.edtSQL);

  mBtnAdd = (Button) findViewById(R.id.btnAdd);
  mBtnUpdate = (Button) findViewById(R.id.btnUpdate);
  mBtnDelete = (Button) findViewById(R.id.btnDelete);
  mBtnSQL = (Button) findViewById(R.id.btnSQL);

  mBtnAdd.setOnClickListener(onClickBtnAdd);
  mBtnUpdate.setOnClickListener(onClickBtnUpdate);
  mBtnDelete.setOnClickListener(onClickBtnDelete);
  mBtnSQL.setOnClickListener(onClickBtnSQL);

 }

 /* 新增 */
 private Button.OnClickListener onClickBtnAdd = new Button.OnClickListener() {
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub

   /* 方法1 直接下指令 */
   if (mEdtName.getText().toString().isEmpty() == false) { // name有值
    db.execSQL("insert into user('name') values ('"
      + mEdtName.getText().toString() + "')");
   }
   if (mEdtAddr.getText().toString().isEmpty() == false) { // address有值
    db.execSQL("insert into location('address') values ('"
      + mEdtAddr.getText().toString() + "')");
   }

   /* 方法2 */
   // if(mEdtName.getText().toString().isEmpty() == false){ // name有值
   // ContentValues newRow = new ContentValues();
   // newRow.put("name", mEdtName.getText().toString());
   // db.insert("user", null, newRow);
   // }
   // if (mEdtAddr.getText().toString().isEmpty() == false) {
   // //address有值
   // ContentValues newRow = new ContentValues();
   // newRow.put("address", mEdtAddr.getText().toString());
   // db.insert("location", null, newRow);
   // }
   queryList();
  }
 };

 /* 修改 */
 private Button.OnClickListener onClickBtnUpdate = new Button.OnClickListener() {
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub

   /* 方法1 直接下指令 */
   db.execSQL("UPDATE location SET address='"
     + mEdtUpdate2.getText().toString() + "' WHERE address='"
     + mEdtUpdate1.getText().toString() + "'");

   /* 方法2 */
   // ContentValues newRow3 = new ContentValues();
   // newRow3.put("address", mEdtUpdate2.getText().toString());
   // db.update("location", newRow3,
   // "address='"+mEdtUpdate1.getText().toString()+"'", null);

   queryList();
  }
 };

 /* 刪除 */
 private Button.OnClickListener onClickBtnDelete = new Button.OnClickListener() {
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub

   /* 方法1 直接下指令 */
   db.execSQL("DELETE FROM location WHERE address='"
     + mEdtDelete.getText().toString() + "'");

   /* 方法2 */
   // db.delete("location",
   // "address='"+mEdtDelete.getText().toString()+"'", null);

   queryList();
  }
 };

 private Button.OnClickListener onClickBtnSQL = new Button.OnClickListener() {
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub

   Cursor cursor = db.rawQuery(mEdtSQL.getText().toString(), null); // 搜尋用rawQuery
   cursor.moveToFirst();
   mTextList1.setText(cursor.getString(0) + cursor.getString(1));
   while (cursor.moveToNext())
    mTextList1.append("\n" + cursor.getString(0) + "    "
      + cursor.getString(1));
  }
 };

 /* 搜尋 */
 void queryList() {

  /* 方法1 直接下指令 */
  String queryUser = "SELECT * FROM user";
  Cursor cursor1 = db.rawQuery(queryUser, null); // 搜尋用rawQuery
  String queryLocation = "SELECT * FROM location";
  Cursor cursor2 = db.rawQuery(queryLocation, null); // 搜尋用rawQuery
  /* 方法2 */
  // Cursor cursor1 = db.query("user", new String[] { "Id", "name"},
  // null, null, null, null, null);
  // Cursor cursor2 = db.query("location", new String[] { "Id",
  // "address"},
  // null, null, null, null, null);

  cursor1.moveToFirst();
  mTextList1
    .setText(cursor1.getString(0) + "    " + cursor1.getString(1));
  while (cursor1.moveToNext())
   mTextList1.append("\n" + cursor1.getString(0) + "    "
     + cursor1.getString(1));

  cursor2.moveToFirst();
  mTextList2
    .setText(cursor2.getString(0) + "    " + cursor2.getString(1));
  while (cursor2.moveToNext())
   mTextList2.append("\n" + cursor2.getString(0) + "    "
     + cursor2.getString(1));
 }

 /* 離開程式 */
 public boolean onKeyDown(int keycode, KeyEvent event) {
  if (keycode == KeyEvent.KEYCODE_BACK) {
   // 關閉資料庫
   dbHelper.closeDatabase();
   finish();
   return true;
  }
  return super.onKeyDown(keycode, event);
 }

 @Override
 public void onDestroy() {
  super.onDestroy();
  // Kill myself
  android.os.Process.killProcess(android.os.Process.myPid());
 }

}
4.main.xml配置


    

        

        
    

    

        

        
    

    

    

        

        

        

        
    

   

    

        

        

        
    

    

    

        

        
    

    

    

        

            

                

                
           
        
    


5.完成如圖


SQLite db2.db 檔案下載
參考來源:
http://fecbob.pixnet.net/blog/post/43610863
http://hscc.cs.nctu.edu.tw/~lincyu/Android/Chapter11.pdf

2014年11月3日 星期一

Android RatingBar Customize 自製 評分

Android內建評分星星系統,可自行製作圖案代替星星
1.activity_main.xml
android:numStars="5" ----- 總數量
android:rating="2.5" ----- 初始值
android:stepSize="0.5" " -----評分單位
style="?android:attr/ratingBarStyleIndicator" ----- 指標(無法拉動)
style="?android:attr/ratingBarStyleSmall" ----- 小指標
style="@style/foodRatingBar" ----- 自製圖案
 




                

                

                

                

                

                

               
            
2.在res -> value -> styles.xml檔案內加上

3.在res -> drawable-hdpi 建立三個檔案
food_ratingbar_full.xml
food_ratingbar_full_empty.xml
food_ratingbar_full_filled.xml
並將圖片

下載至drawable-hdpi此資料夾內
food_ratingbar_full.xml

    
    
    


food_ratingbar_full_empty.xml


    
    
    
    


food_ratingbar_full_filled.xml


 
    
 
    
 
    
 
    
 

4.MainActivity.java
public class MainActivity extends FragmentActivity {

 private RatingBar ratingBar4;
 private TextView txtRatingValue;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  addListenerOnRatingBar();
 }

 //滑動直接顯示分數
 public void addListenerOnRatingBar() {
   
  ratingBar4 = (RatingBar) findViewById(R.id.ratingBar4);
  txtRatingValue = (TextView) findViewById(R.id.txtRatingValue);
  
  //if rating value is changed,
  //display the current rating value in the result (textview) automatically
  ratingBar4.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
   public void onRatingChanged(RatingBar ratingBar, float rating,
    boolean fromUser) {
  
    txtRatingValue.setText(String.valueOf(rating));
  
   }
  });
   }
}


參考來源:
http://www.mkyong.com/android/android-rating-bar-example/
http://kozyr.zydako.net/2010/05/23/pretty-ratingbar/
https://github.com/kozyr/foody-memories

2014年10月1日 星期三

Android automatic send e-mail 自動發送e-mail

也就是不需經過使用者即可發送email,是寄件者(開發者)寄送給其他收件者的方法
1.安裝jar檔
mail.jar
activation.jar
additionnal.jar
 將這三個檔案下載到專案的libs資料夾上,接著在eclips重新整理專案,在這三個檔案按右鍵->Bulid Path-> Add to Build Path 完成
 2.在src新增建立資料夾com,在資料夾com裡面再建立一個資料夾provider,接著再eclips裡此資料夾下建立 JSSEProvider.java
 
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * @author Alexander Y. Kleymenov
 * @version $Revision$
 */


import java.security.AccessController;
import java.security.Provider;

public final class JSSEProvider extends Provider {

    public JSSEProvider() {
        super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
        AccessController.doPrivileged(new java.security.PrivilegedAction() {
            public Void run() {
                put("SSLContext.TLS",
                        "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
                put("Alg.Alias.SSLContext.TLSv1", "TLS");
                put("KeyManagerFactory.X509",
                        "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
                put("TrustManagerFactory.X509",
                        "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
                return null;
            }
        });
    }
}
3.在自己的專案src資料夾下建立GMailSender.java
import javax.activation.DataHandler;   
import javax.activation.DataSource;   
import javax.mail.Message;   
import javax.mail.PasswordAuthentication;   
import javax.mail.Session;   
import javax.mail.Transport;   
import javax.mail.internet.InternetAddress;   
import javax.mail.internet.MimeMessage;   

import android.util.Log;

import java.io.ByteArrayInputStream;   
import java.io.IOException;   
import java.io.InputStream;   
import java.io.OutputStream;   
import java.security.Security;   
import java.util.Properties;   

public class GMailSender extends javax.mail.Authenticator {   
    private String mailhost = "smtp.gmail.com";   
    private String user;   
    private String password;   
    private Session session;   

    static {   
        Security.addProvider(new com.provider.JSSEProvider());   
    }  

    public GMailSender(String user, String password) {   
        this.user = user;   
        this.password = password;   
        Log.i("user::::::::", user);
        Log.i("password::::::::", password);
        Properties props = new Properties();   
        props.setProperty("mail.transport.protocol", "smtp");   
        props.setProperty("mail.host", mailhost);   
        props.put("mail.smtp.auth", "true");   
        props.put("mail.smtp.port", "465");   
        props.put("mail.smtp.socketFactory.port", "465");   
        props.put("mail.smtp.socketFactory.class",   
                "javax.net.ssl.SSLSocketFactory");   
        props.put("mail.smtp.socketFactory.fallback", "false");   
        props.setProperty("mail.smtp.quitwait", "false");   

        session = Session.getDefaultInstance(props, this);   
    }   

    protected PasswordAuthentication getPasswordAuthentication() {   
        return new PasswordAuthentication(user, password);   
    }   

    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {   
        try{
          Log.i("subject::::::::", subject);
             Log.i("body::::::::", body);
             Log.i("sender::::::::", sender);
             Log.i("recipients::::::::", recipients);
        MimeMessage message = new MimeMessage(session);   
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));   
        message.setSender(new InternetAddress(sender));   
        message.setSubject(subject);   
        message.setDataHandler(handler);  

        /*******2015.09.29 附加圖片*******/
        // create multipart
        Multipart multipart = new MimeMultipart();

        // create bodypart with image and set content-id
        MimeBodyPart messageBodyPart = new MimeBodyPart();
        File testImage = new File("/storage/emulated/0/Download/", "test.png"); //手機檔案位置,檔案名稱
        DataSource source = new FileDataSource(testImage);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName("image.png");
        messageBodyPart.setDisposition(MimeBodyPart.INLINE);
        messageBodyPart.setHeader("Content-ID","");
        multipart.addBodyPart(messageBodyPart);

        // create bodypart with html content and reference to the content-id
        messageBodyPart = new MimeBodyPart();
        String htmlText = "";
        messageBodyPart.setContent(htmlText, "text/html");
        multipart.addBodyPart(messageBodyPart);

        // add multipart to message
        message.setContent(multipart);
        
        /***************參考來源 http://goo.gl/OAZn5q ****************/

 
        if (recipients.indexOf(',') > 0)   
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
        else  
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   
        Transport.send(message);   
        }catch(Exception e){
          Log.i("eeeeee::::::::", e+"");
        }
    }   

    public class ByteArrayDataSource implements DataSource {   
        private byte[] data;   
        private String type;   

        public ByteArrayDataSource(byte[] data, String type) {   
            super();   
            this.data = data;   
            this.type = type;   
        }   

        public ByteArrayDataSource(byte[] data) {   
            super();   
            this.data = data;   
        }   

        public void setType(String type) {   
            this.type = type;   
        }   

        public String getContentType() {   
            if (type == null)   
                return "application/octet-stream";   
            else  
                return type;   
        }   

        public InputStream getInputStream() throws IOException {   
            return new ByteArrayInputStream(data);   
        }   

        public String getName() {   
            return "ByteArrayDataSource";   
        }   

        public OutputStream getOutputStream() throws IOException {   
            throw new IOException("Not Supported");   
        }   
    }   
}  
4.接著在想觸發的檔案上加上此段code
try {   
                    GMailSender sender = new GMailSender("username@gmail.com", "password"); //寄件者(開發方)帳號與密碼
                    sender.sendMail("This is Subject",   //信件標題
                            "This is Body",   //信件內容
                            "username@gmail.com",   //寄件者
                            "user@yahoo.com");   //收件者
                } catch (Exception e) {   
                    Log.e("SendMail", e.getMessage(), e);   
                } 
5.可能發生問題在 Log.i("eeeeee::::::::", e+"");
A.android.os.NetworkOnMainThreadExceptionMainActivity.java加上此段
/* 加入StrictMode避免發生 android.os.NetworkOnMainThreadException */
  StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
    .detectDiskReads().detectDiskWrites().detectNetwork()
    .penaltyLog().build());
B.javax.mail.AuthenticationFailedException 此為無法登入帳號,可能是帳號密碼錯誤,或是帳號未設定 此時GMail會收到信,點選連結變更設定
https://www.google.com/settings/security/lesssecureapps

 啟動IMAP
 完成
 參考資料:
http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a/2033124#2033124

http://stackoverflow.com/questions/1334802/how-can-i-use-external-jars-in-an-android-project/6859020#6859020

http://bbs.csdn.net/topics/310000387

http://stackoverflow.com/questions/7398382/embedd-inline-images-using-java-mail-in-android

檔案位置:https://github.com/terryyamg/AutoEmail

2014年9月16日 星期二

Android Action Bar search 搜尋

前陣子製作側邊欄,參考[Android]使用 Navigation Drawer 製作側選單(1)[Android]使用 Navigation Drawer 製作側選單(2) ,由於上方有個ActionBar,在這裡面介紹的是按鈕,由於想用類似搜尋功能,於是稍作修改,主要修改地方為"Action Button 建立及點選事件"
1.在資料夾res->menu->main.xml修改



2.在資料夾res->layout->建立search_layout.xml 一個編輯,一個按鈕


    
    

    


3.MainActivity.java修改
 private EditText searchName;
 private String searchObject;
 private Button searchButton;
 // ================================================================================
 // Action Button 建立及點選事件
 // ================================================================================
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.main, menu);
  View v = (View) menu.findItem(R.id.action_search).getActionView();
  //文字編輯部分
  searchName = (EditText) v.findViewById(R.id.search);
  searchName.addTextChangedListener(new TextWatcher() {
   @Override
   public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub
   }

   @Override
   public void beforeTextChanged(CharSequence s, int start, int count,
     int after) {
    // TODO Auto-generated method stub
   }

   @Override
   public void onTextChanged(CharSequence s, int start, int before,
     int count) {
    // TODO Auto-generated method stub
    try {
     searchObject = searchName.getText().toString(); // 取得輸入文字
    } catch (Exception e) {

    }

   }
  });
  //送出部分
  searchButton = (Button) v.findViewById(R.id.searchGo);
  searchButton.setOnClickListener(new Button.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    goSearch(); //要做甚麼
   }
  });

  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {

  // home
  if (drawerToggle.onOptionsItemSelected(item)) {
   return true;
  }

  // action buttons
  switch (item.getItemId()) {
  //用不到,刪掉
  default:
   break;
  }

  return super.onOptionsItemSelected(item);
 }

 void goSearch() {
  if (searchObject == null) {
  //搜尋空值,不做事
  } else {
   Intent intent = new Intent(this, xxxx.class); //前進至xxxx頁面
   intent.putExtra("searchName", searchObject); //傳值
   startActivity(intent); //啟動出發
  }
 }




參考來源:http://blog.stylingandroid.com/basic-actionbar-part5/
http://wptrafficanalyzer.in/blog/adding-custom-action-view-to-action-bar-in-android/

2014年8月21日 星期四

Android Typeface 字型

更改app上的文字字型
首先要下載中文字型
http://lms.ltu.edu.tw/sys/read_attach.php?id=675313
http://lms.ltu.edu.tw/sys/read_attach.php?id=675314
另外提供一個英文字型網站(英文字型多,中文字少)
http://www.fontsaddict.com/

下載完畢後,解壓縮,選擇想要的字型檔(.ttf)
然後再專案裡的assets建立fonts資料夾
想要的字型檔放入fonts資料夾




使用 Typeface 指令即可修改字型
1.activity_main.xml



2.MainActivity.java
 private TextView output;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  /* 字型 */
  Typeface fontch = Typeface.createFromAsset(getAssets(), "fonts/wt034.ttf");
  output = (TextView) findViewById(R.id.output);
  output.setTypeface(fontch);

 }
參考來源:http://key.chtouch.com/ContentView.aspx?P=216
http://abgne.tw/android/android-code-snippets/android-using-custom-fonts.html

2014年8月15日 星期五

Android ViewPager 前導 說明頁

使用app時會出現前導說明頁
整體流程如下圖
首先要修改先前開機畫面SplashScreen.java檔啟動位置
1.SplashScreen.java
 Intent i = new Intent(SplashScreen.this, Leading.class); //啟動完開機畫面SplashScreen.java後跳轉Leading.java
2.加入Leading.java在AndroidManifest.xml

        
        
3.建立leading.xml,lay1.xml,lay2.xml,lay3.xml
 leading.xml 下方的圓點


    

    

        

        

        
    


lay1.xml 說明頁的第一張圖pic1


    


lay2.xml 說明頁的第二張圖pic2


    



lay3.xml 說明頁的第三張圖pic3+開始體驗Button


    

    


4.Leading.java 說明頁面程式碼
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class Leading extends Activity {
 private ViewPager myViewPager; // 頁卡內容
 private List list; // 存放頁卡
 private TextView dot1, dot2, dot3; // 這些點都是文字
 private Button startButton; // 按鈕,開始體驗

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.leading);
  initDot();
  initViewPager();
 }

 private void initDot() {
  dot1 = (TextView) this.findViewById(R.id.textView1); // 這些點都是文字
  dot2 = (TextView) this.findViewById(R.id.textView2);
  dot3 = (TextView) this.findViewById(R.id.textView3);
 }

 private void initViewPager() {
  myViewPager = (ViewPager) this.findViewById(R.id.viewPager);
  list = new ArrayList();

  LayoutInflater inflater = getLayoutInflater();

  View view = inflater.inflate(R.layout.lay3, null); // 只是為了等下findviewbuid而獨立拿出來賦給view

  list.add(inflater.inflate(R.layout.lay1, null));
  list.add(inflater.inflate(R.layout.lay2, null));
  list.add(view);
  try {
   myViewPager.setAdapter(new MyPagerAdapter(list));

   myViewPager.setOnPageChangeListener(new MyPagerChangeListener());
  } catch (NullPointerException e) {
  }
  startButton = (Button) view.findViewById(R.id.start); // 與上面對應,獲取這個按鈕

  startButton.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    Intent intent = new Intent(Leading.this, MainActivity.class); //按下按鈕後跳轉至MainActivity.java

    startActivity(intent);

   }
  });
 }

 class MyPagerAdapter extends PagerAdapter {
  public List mListViews;

  public MyPagerAdapter(List mListViews) {
   this.mListViews = mListViews;
  }

  @Override
  public void destroyItem(View arg0, int arg1, Object arg2) {
   ((ViewPager) arg0).removeView(mListViews.get(arg1));
  }

  @Override
  public void finishUpdate(View arg0) {
  }

  @Override
  public int getCount() {
   return mListViews.size();
  }

  @Override
  public Object instantiateItem(View arg0, int arg1) {
   ((ViewPager) arg0).addView(mListViews.get(arg1), 0);
   return mListViews.get(arg1);
  }

  @Override
  public boolean isViewFromObject(View arg0, Object arg1) {
   return arg0 == (arg1);
  }

  @Override
  public void restoreState(Parcelable arg0, ClassLoader arg1) {
  }

  @Override
  public Parcelable saveState() {
   return null;
  }

  @Override
  public void startUpdate(View arg0) {
  }
 }

 class MyPagerChangeListener implements OnPageChangeListener {

  @Override
  public void onPageSelected(int arg0) {
   // TODO Auto-generated method stub
   switch (arg0) { // 設置點的顏色
   case 0:
    dot1.setTextColor(Color.WHITE);
    dot2.setTextColor(Color.BLACK);
    dot3.setTextColor(Color.BLACK);
    break;

   case 1:
    dot1.setTextColor(Color.BLACK);
    dot2.setTextColor(Color.WHITE);
    dot3.setTextColor(Color.BLACK);
    break;

   case 2:
    dot1.setTextColor(Color.BLACK);
    dot2.setTextColor(Color.BLACK);
    dot3.setTextColor(Color.WHITE);
    break;

   }
  }

  @Override
  public void onPageScrollStateChanged(int arg0) {
   // TODO Auto-generated method stub

  }

  @Override
  public void onPageScrolled(int arg0, float arg1, int arg2) {
   // TODO Auto-generated method stub

  }

 }
}

參考來源:http://blog.segmentfault.com/zhongbaitu/1190000000398209

2014年8月4日 星期一

Android 上架至 google play 問題

基本上參考此連結http://xyz.cinc.biz/2013/06/android-app.html
寫得很清楚,但步驟3完會出現strings.xml錯誤的訊息

找了一下

點選eclipse的 "Window" > "Preferences" > "Android" > "Lint Error Checking"
找到 Id欄 是"MissingTranslation" 點選後Erroe修改成Warning就可以了

參考連結:
http://stackoverflow.com/questions/11443996/lint-how-to-ignore-key-is-not-translated-in-language-errors