我有一個帶5個按鈕的主屏幕。每次按一個按鈕我想要一個新的屏幕。到目前爲止,我還有其他5個類(每個屏幕)和5 xmls。但Iam肯定會有更好的方式,因爲這個屏幕和xml是相同的,我想要做的是改變一些文本和一些數據從數據庫中獲取。我相信我可以繼續使用另一個類,只有一個xml,然後傳遞我想要的值作爲參數。 (想象一下,在最終狀態下,我的應用程序必須有15個按鈕,所以我認爲這太浪費空間了,並且沒有必要擁有15個.java文件和15個xml文件,這些文件看起來是一樣的,只有圖像和文本視圖的某些值會發生變化) 。我的主要活動代碼:讓我的課變成一個參數
public class MyActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
main2Theme();}
private void main2Theme() {
setContentView(R.layout.main_new);
Button Button100 = (Button)findViewById(R.id.Button100);
Button113.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(this, OtherScreenName.class);
startActivity(i);
}
}); //and so on for 15 buttons
我OtherScreenName.class是:
public class OtherScreenName extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Theme();
}
@SuppressWarnings("null")
private void Theme() {
Log.d("SnowReportApp","Do first thing");
setContentView(R.layout.otherscreenname); //THIS CHANGES IN EVERY CLASS DEPENDING ON THE ID OF THE BUTTON
String result = "";
InputStream is = null;
StringBuilder sb=null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();//() before
nameValuePairs.add(new BasicNameValuePair("ski_id","OtherScreenName"));
TextView text1 = (TextView) findViewById(R.id.otherscreenname_1);
//THESE 2 CHANGE DEPERNDING ON THE BUTTON PRESSED
//perform action.....
//AND ALSO HERE I NEED THE ID OF THE BUTTON
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(OtherScreenName.this,MyActivity.class);
startActivity(i);
}
});
任何人都可以提出如何給參數上我的課,什麼應該是他們的類型?
就是這樣。因此,儘管我想傳遞的類型的數量和參數,我創建了一個包,然後說b.putInt,b.putInt作爲第二個包,以此類推? (或每個參數不同的捆綁包?)非常感謝! – ghostrider 2012-01-10 22:06:57
是的,確切地說。當然,如果你有很多想要傳遞的參數,比將它們包裝到實現Parcelable接口的類中並將它放到Extras中'b.putExtra(String name,Parcelable value)' – vitakot 2012-01-10 22:22:43