2013-01-09 57 views
0

我創建一個Android應用程序中,我有一個包含幾個元素,比如開關一活動,TextView的等過濾器從XML文件

其他元素的列表中的一些器開關元件我需要從所有交換機獲取值元素是否被選中。

如果有人向我展示將多個值傳遞給不同活動進行進一步計算的正確方法,那將會很棒。

感謝

這裏是我的XML代碼:

  <LinearLayout 
       android:id="@+id/configSwitchHldr" 
       android:layout_marginTop="10dp" 
       android:layout_span="2" 
       android:gravity="center" 
       android:orientation="vertical" 
       android:paddingLeft="30dp" 
       android:paddingRight="30dp" > 

       <TextView 
        android:text="@string/coreHardware" 
        android:layout_width="fill_parent" 
        android:layout_height="20dp" 
        android:textColor="@color/green" 
        android:layout_marginTop="10dp" 
        android:layout_marginBottom="10dp" 
        /> 
       <View 
        android:layout_width="fill_parent" 
        android:layout_height="2dp"  
        android:background="#363636" /> 
       <Switch 
        android:id="@+id/switch1" 
        android:layout_width="fill_parent" 
        android:layout_height="14.5sp" 
        android:textOn="ON" 
        android:textOff="OFF" 
        android:text="CPU"    
        android:paddingLeft="5dp" 
        android:paddingRight="5dp" 
        android:layout_marginTop="10dp" 
        android:layout_marginBottom="10dp" 
        /> 
       <View 
        android:layout_width="fill_parent" 
        android:layout_height="1dp"  
        android:background="#363636" /> 

       <Switch 
        android:id="@+id/switch2" 
        android:layout_width="fill_parent" 
        android:layout_height="14.5sp" 
        android:textOn="ON" 
        android:textOff="OFF" 
        android:text="Memory" 
        android:paddingLeft="5dp" 
        android:paddingRight="5dp" 
        android:layout_marginTop="10dp" 
        android:layout_marginBottom="10dp"/> 

       <View 
        android:layout_width="fill_parent" 
        android:layout_height="1dp"  
        android:background="#363636" /> 

       <Switch 
        android:id="@+id/switch3" 
        android:layout_width="fill_parent" 
        android:layout_height="14.5sp" 
        android:textOn="ON" 
        android:textOff="OFF" 
        android:text="Storage" 
        android:paddingLeft="5dp" 
        android:paddingRight="5dp" 
        android:layout_marginTop="10dp" 
        android:layout_marginBottom="10dp"/> 

       <View 
        android:layout_width="fill_parent" 
        android:layout_height="1dp"  
        android:background="#363636" /> 

      </LinearLayout> 
+0

你是什麼意思:「如果有人向我展示將多個值傳遞給不同活動進行進一步計算的正確方法,那將是非常棒的。」 您想從中獲取多少個小工具?有時,創建一些獲取所有值的泛型方法比獲取當前值時更加麻煩... – Darwind

+0

我將從用戶輸入一個活動,然後將這些值傳輸到同一應用程序中的另一個活動以進行進一步處理。 我在一個活動中有多個開關元素。我想獲得開關元素的所有值(是或否)將其發送到另一個活動進行處理。 –

回答

0

好了,根據你的評論這是怎麼可以這樣做:

在你的第一個Activity,你需要一個方法,收集所有開關狀態並將它們傳遞給下一個活動。將參數傳遞給其他活動可以通過將它們放入您發送的Intent以打開新的Activity來實現。

用於獲取Switches狀態並將它們傳遞到一個新的Activity將大致是這個樣子的方法:

Intent i = new Intent(MainActivity.this, NewActivity.class); 
i.putExtra("switch1", switch1.isChecked()); 
i.putExtra("switch2", switch2.isChecked()); 
i.putExtra("switch3", switch3.isChecked()); 
startActivity(i); 

把這個onClickListener內的Button比如像這樣:

button1 = (Button) findViewById(R.id.button1); 
button1.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Intent i = new Intent(MainActivity.this, NewActivity.class); 
     i.putExtra("switch1", switch1.isChecked()); 
     i.putExtra("switch2", switch2.isChecked()); 
     i.putExtra("switch3", switch3.isChecked()); 
     startActivity(i); 
    } 
}); 

現在當你點擊Button你會被髮送到NewActivity。通過將它們的布爾數組內

boolean bool1 = getIntent().getExtras().getBoolean("switch1"); 
boolean bool2 = getIntent().getExtras().getBoolean("switch2"); 
boolean bool3 = getIntent().getExtras().getBoolean("switch3"); 

你可以通過從MainActivity變量:

爲了取回在MainActivity傳遞的參數,你應該做這樣的事情在你的NewActivity的onCreate方法,但這在我看來是過分的。

也看看這SO貼:How do I pass data between Activities in Android application? - 有很多這些。