2011年3月19日 星期六

頁面轉換

如果你做的是需要轉換頁面的程式,整理一下大約有以下幾種,分別是
#純換衣服setContentView
#帶傘出門 (Bundle+startActivity)
#出門不帶傘 (startActivity)
#出門帶傘且記得帶回家 (Bundle+startActivityForResult)

#純換衣服setContentView(只是原頁轉換layout,非轉換頁面. 不能傳輸資料,須先製作play.xml)
 setContentView(R.layout.play);

#帶傘出門 (傘=Bundle)
Intent (傳值) , 新的Activity  >> startActivity
 PS:新的Activity必須在Manifest裡加上<activity android:name="PlayRadio"></activity>才可以

 <<原頁 KRadio.java>>
 Intent intent = new Intent();
 intent.setClass(KRadio.this, PlayRadio.class);
 Bundle bundle = new Bundle();
 bundle.putString("RadioChannel",textview1.getText().toString());
 intent.putExtras(bundle);
 startActivity(intent);

 <<目的頁 PlayRadio.java>>
 Bundle bundle = this.getIntent().getExtras();
 String str = bundle.getString("RadioChannel");

#出門不帶傘 (不需要Bundle)
 <<原頁 KRadio.java>>
//不傳值三行即可
     Intent intent = new Intent();
     intent.setClass(KRadio.this, Search.class);
     startActivity(intent);
     //finish();    加了finish會關掉Activity按<-就回不了家

 <<目的頁 PlayRadio.java>>
//不傳值就不需要寫接值的code啦

#出門帶傘且記得帶回家 (Bundle+startActivityForResult)
 <<原頁>>
 Intent intent = new Intent();
   intent.setClass(LocationList.this, Search.class);
   Bundle bundle = new Bundle();
   bundle.putString("whereCol",_FIELD);
   bundle.putString("whereVar",paths.get(position).toString());
   intent.putExtras(bundle);
   startActivityForResult(intent,0);
 //原頁要接目的頁原來的值要用onActivityResult
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data)
 switch (resultCode)
  case RESULT_OK:
   Bundle bunde = data.getExtras();
   String whereCol = bunde.getString("whereCol");
 break;
 <<目的頁>>
 Intent intent;

 intent = this.getIntent();
 Bundle bundle = intent.getExtras();
 String str1 = bundle.getString("whereCol");
 String str2 = bundle.getString("whereVar");
//回傳
 setResult(RESULT_OK, intent);
 finish();

標籤: , , , , ,

0 個意見:

張貼留言

訂閱 張貼留言 [Atom]

<< 首頁