發表文章

「Android誌」 Button事件處理、設定監聽事件

在我們寫AndroidApp時、Button一定會常常用到、現在讓我們來試試Button的監聽事件、已下有幾個方法可以達到監聽、記住Android規定只要有設監聽( setOnClickListener )、就必需一定要有點擊事件( onClick )。 方法一、直接將Button設為監聽事件 public class MainActivity extends AppCompatActivity{ //宣告一個TextView名為txv TextView txv; //宣告Button Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //設定目前的View為activity_main setContentView(R.layout.activity_main); txv = (TextView) findViewById(R.id.textView); //設定Button btn1 = (Button) findViewById(R.id.btn1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { txv.setText("1"); } }); } } 但如果你同時有非常多Button都必需設監聽、那不就每個Button都必需設監聽然後在設定點擊事件( onClick )、假如你這樣做那麼在後面程式上的修改及閱讀上都會非常困難、解決的方法可以將整個class都設為同一點擊事件、在利用 switch 來選擇Button的 id 。 方法二、將整個class設為同一事件 public class MainActivity extends A...

「Android誌」 FrameLayout 佈局

圖片
FrameLayout是所有的Layout裡最單純的一個,在FrameLayout裡面假設有一個以上的元件,則會以在最上層的元件為主,假若有數個元件,其大小皆相同,此時只會看到第一個元件,其他元件會被蓋在下面,而無法顯現在畫面中;若是不同大小的元件,則會看到由下到上的元件。 JAVA package com.example.administrator.sqlite; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } XML <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.sqlite.MainActivity"> <TextView android:layout_width="fill_parent" android:layout_height="fill_...

「Android誌」為程式碼自動上色

<pre class="codeblock prettyprint linenums:1"> 中間可以插入你想放入的程式碼 </pre>

「Android誌」Android int to string, string to int, 整數轉字串、字串轉整數

整數轉字串 String stringValue = Integer.toString(12345); String stringValue = String.valueOf(12345); 字串轉整數 int intValue = Integer.valueOf("12345"); int intValue = Integer.parseInt("12345");

「Android誌」Android處理SSL驗證

錯誤代碼 javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. ---------------------------------------------------------------------------------------------------------------------- 解決方式 在製做電子發票驗證時、由於遇到網站憑證的問題、處理方式為避開SSL驗證 使用時先調用 requestWithoutCA 、再使用httpsurlconnection。 public void requestWithoutCA() { try { SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, new TrustManager[] { new MyTrustManager() }, new SecureRandom()); HttpsURLConnection .setDefaultSSLSocketFactory(sc.getSocketFactory()); HttpsURLConnection .setDefaultHostnameVerifier(new MyHostnameVerifier()); URL url = new URL("https://certs.cac.washington.edu/CAtest/"); HttpURLConnection urlConnection = (HttpURLConnection) url .openConnection(); InputStream in = urlConnection.getInputStream(); // 取得输入流,并使用Reader读取 BufferedReader reader = new BufferedReader( new InputStreamR...

「Android誌」BloggerJizz 開始啦

因為在寫程式時常常需要參考到非常多的資料,於是就開創一個新的部落格來記錄一下~