綁定監聽器:
listener1 = new onClickListener(){
public void onClick(View v){
TextView text_view = (TextView) findViewById(R.id.TextView01);
}
}
setContentView(R.layout.main);
button1 = (Button)findViewById(R.id.Button01);
button1.setOnClickListener(listener1);
另一種寫法
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
}
});
對話框
1. Toast
Toast.makeText(this, "顯示字串", Toast.LENGTH_SHORT).show();
2.popupWindow
final PopupWindow popupWindow = new PopupWindow(PopupActivity.this);
popupWindow.setContentView(button);
popupWindow.setFocusable(true);
popupWindow.setWidth(200);
popupWindow.setHeight(100);
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
//按下對話視窗關閉PopupWindow視窗
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
popupWindow.dismiss();
}
});
3.dialog
final Dialog dialog = new Dialog(PopupActivity.this);
dialog.setTitle("這裡可以用來顯示Dialog信息!");
dialog.setContentView(button);
dialog.show();
//按下對話視窗上的按鈕來關閉Dialog視窗
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
4.AlertDialog
Builder builder = new Builder(PopupActivity.this);
builder.setTitle("AlertDialog");
builder.setMessage("這裡可以用來顯示Alert信息,按[關閉]鍵會自動關閉");
builder.setPositiveButton("關閉", null);
builder.show();
break;
5.ProgressDialog
final ProgressDialog progressDialog = ProgressDialog.show(PopupActivity.this, "處理中...", "請等一會,處理完畢會自動結束...");
final Handler handler = new Handler();
//建立處理程式callback
final Runnable callback = new Runnable() {
public void run() {
progressDialog.dismiss();
}
};
//建立一個Thread來Run,當處理進度完畢時,執行callback程式來關閉ProgreeDialog視窗
Thread thread = new Thread() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(callback);
}
};
thread.start();
Intent採取動作或資料處理
Action: MAIN、VIEW、EDIT、CALL
資料處理: URI、MIME
畫面切換
Intent intent = new Intent(Main.this, Chatter.class);
startActivity(intent);
畫面切換後,等待回應訊息
Intent intent = new Intent(Main.this, Chatter.class);
startActivityForResult(intent, SHOW_EDITOR);
Intent之間資料傳遞
EditText editText = (EditText)findViewById(R.id.EditText01);
CharSequence text = editText.getText();
intent.putExtra("TEXT", text);
TEXT = "Sending Data"
Bundle extras = getIntent().getExtra();
if(extras != null){
EditText editText = (EditText)findViewById(R.id.EditText01);
editText.setText(extras.getcharSequence("TEXT"));
}
Spinner選單
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.actions, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner = (Spinner)findViewById(R.id.Spinner01);
spinner.setAdapter(adapter);
Spinner spinner = (Spinner)findViewById(R.id.Spinner01);
//設定Intent的動作(Action)和Uri
Intent intent = new Intent(spinner.getSelectedItem().toString(), Uri.parse(editText.getText().toString()));
try {
EditText editText = (EditText)findViewById(R.id.EditText01);
Spinner spinner = (Spinner)findViewById(R.id.Spinner01);
//設定Intent的動作(Action)和Uri
Intent intent = new Intent(spinner.getSelectedItem().toString(),Uri.parse(editText.getText().toString()));
//Start Activity,執行Browser
startActivity(intent);
} catch (Exception e) {
TextView textView = new TextView(Main.this);
textView.setText(e.getMessage());
Dialog dialog = new Dialog(Main.this);
dialog.setTitle(e.getClass().getName());
dialog.setContentView(textView);
dialog.show();
}
list選項放入陣列 設定選擇
final String[] layouts = {"A" , "B" , "C" , "D",};
//將4個範例選單名稱layouts安置在畫面佈局ListView01
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, layouts);
ListView listView = (ListView)findViewById(R.id.ListView01);
listView.setAdapter(adapter);
//按下選單名稱指向相關的應用程式Class
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
Intent intent = new Intent(LayoutExample.this, Class.forName(getClass().getPackage().getName()
+ "." + layouts[position] + "Activity"));
startActivity(intent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
});
list表單
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_list_item_1, list);
ListView listView = (ListView)findViewById(R.id.ListView01);
listView.setAdapter(adapter);
//按下選單名稱指向相關的應用程式Class
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Widgets.this, (Class<?>)activities[position * 2 + 1]);
startActivity(intent);
}
});
Menu選單()
<menu
<item android:id = "@+id/new_item01"
android:title = "MENU_ITEM_1"/>
<item android:id = "@+id/new_item02"
android:title = "MENU_ITEM_2"/>
</menu>
掛載選單
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
判斷選擇項目
publci boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case Menu_ITEM_ID1:
newHandle01();
return true;
case Menu_ITEM_ID2:
newHandle02();
return true;
}
return true;
}
分享優先資料 Shared Preferences
寫入:
public String SETTING_PREF = "SETTING_Pref"; //定義SharedPreferences內容檔名
public String SHARED_MSG1 = "Shared_Msg1"; //定義字串變數-1
public String SHARED_MSG2 = "Shared_Msg2";
SharedPreferences settings = getSharedPreferences(SETTING_PREF, 0);
int int2 = Integer.parseInt(mEditText02.getText().toString());
settings.edit()
.putString(SHARED_MSG1, mEditText01.getText().toString())
.putInt(SHARED_MSG2, int2)
.commit();
讀取:
SharedPreferences settings = getSharedPreferences(SETTING_PREF, 0);
String msg1 = settings.getString(SHARED_MSG1, "");
mEditText01.setText(msg1);
int defint2 = 0; //存取int型別資料
int msg2_int = settings.getInt(SHARED_MSG2, defint2);
String msg2 = String.valueOf(msg2_int);
mEditText02.setText(msg2);
沒有留言:
張貼留言