1.在測試時,如何實現一個提示可以使用
Java代碼:
- Toast.makeText(this, "這是一個提示", Toast.LENGTH_SHORT).show();
- //從資源文件string.XML 裡面取提示信息
- Toast.makeText(this, getString(R.string.welcome), Toast.LENGTH_SHORT).show();
- //這個提示會幾秒鐘後消失
2.可以使用AlertDialog.Builder 才產生一個提示框.例如像messagebox那樣的
Java代碼:
- new AlertDialog.Builder(this) .setTitle("android 提示") .setMessage("這是一個提示,請確定") .show();
- //帶一個確定的對話框
- new AlertDialog.Builder(this) .setMessage("這是第二個提示") .setPositiveButton("確定",
- new DialogInterface.OnClickListener(){
- public void onClick(DialogInterface dialoginterface, int i){
- //按鈕事件
- }
- }) .show();
- AlertDialog.Builder 還有很多復雜的用法,有確定和取消的對話框
- new AlertDialog.Builder(this) .setTitle("提示") .setMessage("確定退出?") .setIcon(R.drawable.quit) .setPositiveButton("確定", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- setResult(RESULT_OK);//確定按鈕事件
- finish();
- }
- }) .setNegativeButton("取消", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- //取消按鈕事件
- }
- }) .show();
3.menu 的用法.
Java代碼:
- public static final int ITEM_1_ID = Menu.FIRST;
- public static final int ITEM_2_ID = Menu.FIRST + 1;
- public static final int ITEM_3_ID = Menu.FIRST + 2;
- public boolean onCreateOptionsMenu(Menu menu) {
- super.onCreateOptionsMenu(menu);
- //不帶圖標的menu
- menu.add(0, ITEM_1_ID, 0, "item-1");
- //帶圖標的menu
- menu.add(0, ITEM_2_ID, 1, "item-2").setIcon(R.drawable.editbills2);
- menu.add(0, ITEM_3_ID, 2, "item-3").setIcon(R.drawable.billsum1);
- return true;
- }
- public boolean onOptionsItemSelected(MenuItem item){
- switch (item.getItemId()) {
- case 1:
- Toast.makeText(this, "menu1",Toast.LENGTH_SHORT).show();
- return true;
- case 2:
- return true;
- case 3:
- return true;
- }
- return false;
- }
4.Activity 的切換
Java代碼:
- //2個Activity 的切換,沒有數據傳遞
- //從A到B
- Intent intent = new Intent();
- intent.setClass(A.this, B.class);
- startActivity(intent);
- //2個Activity 之間傳遞數據
- //相關的幾個函數
- startActivityForResult
- public final void setResult(int resultCode, String data)
- //回調函數
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- //例如A到B,從B得到數據
- //A到B
- static final int RG_REQUEST = 0;
- Intent intent = new Intent();
- intent.setClass(A.this, B.class);
- startActivityForResult(intent,RG_REQUEST);
- //在B中處理
- Bundle bundle = new Bundle();
- bundle.putString("DataKey", edittext.getText().toString());//給bundle 寫入數據
- Intent mIntent = new Intent();
- mIntent.putExtras(bundle);
- setResult(RESULT_OK, mIntent);
- finish();
- //最後在A的回調函數裡面接收數據
- if (requestCode == RG_REQUEST) {
- if (resultCode == RESULT_CANCELED)
- setTitle("Canceled...");
- else if(resultCode == RESULT_OK) {
- setTitle((String)data.getCharSequenceExtra("DataKey"));
- }
- }