我已經創建了一個XML頁面,其中包含2個文本視圖和一個沒有ID的seekbar。 CustomSeekBar類使用xml頁面作爲基本結構創建這些對象。 您可以在我的模擬器上看到textviews的空間,但我很難搞清楚如何設置文本。很明顯,我錯過了一些東西,因爲CustomSeekBar類沒有辦法告訴我要爲哪些文本視圖設置文本。沒有ID的Android TextView setText
如何在不給每個textview設置硬編碼ID的情況下設置每個單獨視圖的文本? 我之所以說沒有硬編碼ID,是因爲如果每個文本視圖都被命名,那麼當一個文本視圖的文本需要更改時,是不是所有的文本視圖文本都會改變? 由於我的customseekbar類與活動有複合關係,我該如何調用特定的textview ID?
調用一切的活動。
public class ColorsActivity extends ListActivity {
/** Called when the activity is first created. */
//Array Adapter that will hold our ArrayList and display the items on the ListView
SeekBarAdaptor seekBarAdaptor;
//List that will host our items and allow us to modify that array adapter
ArrayList<CustomSeekBar> seekBarArrayList=null;
// TextView myValueText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbarlist);
//Initialize ListView
ListView lstTest= getListView();
//Initialize our ArrayList
seekBarArrayList = new ArrayList<CustomSeekBar>();
//Initialize our array adapter
seekBarAdaptor = new SeekBarAdaptor(ColorsActivity.this, R.layout.seekbars, seekBarArrayList);
CustomSeekBar red = new CustomSeekBar(this, "red", 1);
//CustomSeekBar blue = new CustomSeekBar(this, "blue");
//CustomSeekBar green = new CustomSeekBar(this, "green");
//Set the above adapter as the adapter of choice for our list
lstTest.setAdapter(seekBarAdaptor);
seekBarArrayList.add(red);
//seekBarArrayList.add(blue);
//seekBarArrayList.add(green);
Amarino.connect(this, "00:11:11:21:05:53");
}
}
CustomSeekBar類
public class CustomSeekBar implements SeekBar.OnSeekBarChangeListener {
Context myContext;
TextView myValue;
TextView myLabel;
SeekBar mySeekBar;
CustomSeekBar(Context context, String label, int ID){
myContext = context;
myValue = new TextView(myContext);
mySeekBar = new SeekBar(myContext);
myValue.setText(label);
mySeekBar.setOnSeekBarChangeListener(this);
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
myValue.setText(progress);
Amarino.sendDataToArduino(myContext, "00:11:11:21:05:53", 'A', progress);
}
public void onStopTrackingTouch(SeekBar seekBar){
}
public void onStartTrackingTouch (SeekBar seekBar){
}
}
seekbarlist.xml握住我的自定義列表列表視圖
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
seekbars.xml是每個自定義列表項的結構(CustomSeekBar)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/seekBarLayout">
<TextView
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
/>
<TextView
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
/>
<SeekBar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:max="255"/>
</LinearLayout>
如果你沒有名字,我不認識你,我怎麼會在人羣中打電話給你,給你答案? – 2012-03-28 02:50:53
我給出了一個簡短的解釋和跟進問題。 – Spectrem 2012-03-28 14:24:10