若想處理editText元件中文字變動的事件,
就可實作textWathcer介面,
並用editText元件呼叫addTextChangedListener()註冊textWatcher的監聽,
此範例為監聽editText的更動,並將輸入的密碼顯示於textView上。
Will 發表在 痞客邦 留言(0) 人氣(2,836)
此範例為一個簡單菜單範例,使用者可任意選取所要的項目,
使用者每次選取/取消餐點,就即時記錄已選取的項目,
MainActivity.java
package tw.com.flag.ch05_foodmenuevent;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity implements OnCheckedChangeListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 所有核取方塊 ID 的陣列
int[] chk_id={R.id.chk1,R.id.chk2,R.id.chk3,R.id.chk4,
R.id.chk5,R.id.chk6,R.id.chk7,R.id.chk8};
for(int id:chk_id){ // 用迴圈替所有核取方塊加上監聽物件
CheckBox chk=(CheckBox) findViewById(id);
chk.setOnCheckedChangeListener(this);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void takeOrder(View v) {
String msg=""; // 儲存訊息的字串
for(CompoundButton i:selected) // 以迴圈逐一將換行字元及
msg+="\n"+i.getText(); // 選項文字附加到 msg 字串後面
if(msg.length()>0) // 有點餐
msg ="你點購的餐點是:"+msg;
else
msg ="請點餐!";
// 在文字方塊中顯示點購的項目
((TextView) findViewById(R.id.showOrder)).setText(msg);
}
// 用來儲存已選取項目的 ID 集合物件
ArrayList<CompoundButton> selected=new ArrayList<CompoundButton>();
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked==true) // 若項目被選取
selected.add(buttonView); // 加到集合之中
else // 若項目被取消
selected.remove(buttonView); // 自集合中移除
}
}
Will 發表在 痞客邦 留言(0) 人氣(1,028)
核取方塊(CheckBox)也是一種提供選擇的見面元件,
不同於單選鈕(RadioButton/RadioGroup)一次只能選取一項,
核取方塊(CheckBox)是可提供複選的選擇元件。
檢查核取方塊(CheckBox)是否被選取只要用其物件呼叫isChecked()方法,
Will 發表在 痞客邦 留言(0) 人氣(2,000)
對於RadioButton/RadioGroup元件,較重要的是改變選項的事件,
改變選項需實作RadioGroup.OnCheckedChangeListener,並只需實作一個方法,
onCheckedChanged(RadioGroup group, int checkId)
Will 發表在 痞客邦 留言(0) 人氣(1,227)
單選鈕是指使用者每次只能選擇一個項目的元件,
RadioButton本身並不提供單選的機制,
要讓一組RadioButton每次只有一個能被選取,
就必須將它們放在RadioGroup元件之中。
Will 發表在 痞客邦 留言(0) 人氣(18,361)
此範例為點擊按鈕觸發亂數設定顏色屬性, 最後將隨機產生的RGB
屬定為畫面最下方之之空LinearLayout之背景顏色
MainActivity.java
package com.example.randomcolor;
import java.util.Random;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button randomColorBtn;
TextView txvR, txvG, txvB;
View colorBlock;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
randomColorBtn = (Button)findViewById(R.id.randomColorBtn);
txvR = (TextView)findViewById(R.id.txvR);
txvG = (TextView)findViewById(R.id.txvG);
txvB = (TextView)findViewById(R.id.txvB);
colorBlock = findViewById(R.id.colorBlock);
}
public void changeColor(View v)
{
Random x = new Random();
int red = x.nextInt(256); //取得0~255之間亂數
txvR.setText("RED:"+red); // 顯示亂數值
txvR.setTextColor(Color.rgb(red, 0, 0)); //將顏色設定為亂數紅色值
int green = x.nextInt(256);
txvG.setText("GREEN:"+green);
txvG.setTextColor(Color.rgb(0, green, 0)); //將顏色設定為亂數綠色值
int blue = x.nextInt(256);
txvB.setText("BLUE:"+blue);
txvB.setTextColor(Color.rgb(0, 0, blue)); //將顏色設定為亂數藍色值
colorBlock.setBackgroundColor(Color.rgb(red, green, blue)); //設定最下方空白之linearLayout之背景顏色
}
}
Will 發表在 痞客邦 留言(0) 人氣(808)
此範例為點擊按鈕後, 在textView上顯示user所輸入editText之文字內容
MainActivity.java
package com.example.layout2;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText firstName, lastName, phoneNum;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstName = (EditText)findViewById(R.id.firstName);
lastName = (EditText)findViewById(R.id.lastName);
phoneNum = (EditText)findViewById(R.id.phoneNum);
textView = (TextView)findViewById(R.id.textView4);
}
/*在button properties設定on click屬性, 點擊呼叫此方法*/
public void onClick(View v)
{
/*getText()取得text內容, 並用toString()轉成字串, trim()將字串左右旁的空格擷取掉*/
textView.setText(lastName.getText().toString().trim() +
firstName.getText().toString().trim() +
"'s phoneNumber is" +
phoneNum.getText().toString().trim());
}
}
Will 發表在 痞客邦 留言(0) 人氣(12,494)
在點擊editText之類的物件跳出鍵盤, 輸入資料後若無法關閉
則可取用以下程式碼, 即可關閉虛擬鍵盤
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){
imm.hideSoftInputFromWindow
(getCurrentFocus().getWindowTokeInputMethodManager.HIDE_NOT_ALWAYS);
}
}
return super.onTouchEvent(event);
}
Will 發表在 痞客邦 留言(0) 人氣(5,754)
上一篇提到的簡易型與傳統型處理機制,
其中最大的分別在於簡易型只能用於元件被觸擊的事件處理情況,
而傳統型可用於多種情況, 不只用於觸擊事件,
例如下拉式選單改建 or 其他事件之處理等等, 都可以採用傳統型處理機制
MainActivity.java
Will 發表在 痞客邦 留言(0) 人氣(33)
在layout XML檔中設定button, 並在properties欄位中的on click設定呼叫之方法名稱,
此種事件處理為簡化行處理機制,
此範例中設定此方法名稱為toShowToast,
點擊之後取得按鈕text內容並且用toast方塊顯示
MainActivity.java
Will 發表在 痞客邦 留言(0) 人氣(94)