我已经创建了一个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