一、四種Toast詳解
1.默認效果:
Toast.makeText(getApplicationContext(), "默認Toast樣式",
Toast.LENGTH_SHORT).show();
2.自定義顯示位置效果:
toast = Toast.makeText(getApplicationContext(),
"自定義位置Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
3.帶圖片效果:
toast = Toast.makeText(getApplicationContext(),
"帶圖片的Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
toast.show();
4.完全自定義效果:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom,
(ViewGroup) findViewById(R.id.llToast));
ImageView image = (ImageView) layout
.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定義Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
二、Activity之間傳遞數據
1、向子頁面傳遞數據
父頁面:
Intent intent=new Intent();
intent.setClass(DActivity.this, DetailActivity.class);
intent.putExtra("call", "From DActivity");
startActivity(intent);
子頁面:
Intent intent=new Intent();
intent=getIntent();
CharSequence result= intent.getExtras().getString("call");
2、父頁面從子頁面取返回數據(但如果父頁面在TabHost中,則onActivityResult觸發,不知道爲什麼?)
父頁面:
Intent intent=new Intent();
intent.setClass(MainActivity.this, OtherActivity.class);
intent.putExtra("main", "來自主頁面的參數");
startActivityForResult(intent, 1);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
//super.onActivityResult(requestCode, resultCode, data);
String result = data.getExtras().getString("other");//得到新Activity 關閉後返回的數據
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
Log.i("onActivityResult", result);
}
子頁面:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent();
intent.putExtra("other", "from子頁面");
setResult(1,intent);
finish();
}