1. 首先,在Visual Studio中为您的项目添加一个控件。您可以选择任何您需要的控件类型,例如Label、Button、PictureBox等等。
2. 将控件添加到ListBox控件中,可以通过使用ListBox的Item.Add方法实现。这将在ListBox控件中添加一个新的ListItem对象。
3. 然后,您需要将新添加的控件与ListItem关联。这可以通过设置ListItem.Tag属性来完成。例如,您可以将控件实例赋给ListItem.Tag属性,这样就能够在ListBox中轻松地访问该控件。
4. 最后,您需要更新ListBox控件以显示已添加的控件。这可以通过调用ListBox的Refresh方法来完成。
下面是一个简单的示例代码,它向ListBox控件中添加了一个CheckBox控件:
```
// 创建一个新的CheckBox控件
CheckBox checkBox = new CheckBox()
checkBox.Width = 100
checkBox.Text = 选择
checkBox.Checked = true
// 将新的CheckBox添加到ListBox控件中
listBox1.Items.Add(new ListItem(Item with CheckBox, checkBox))
// 更新ListBox
listBox1.Refresh()
```
当然,如果您需要添加其他类型的控件,只需要按照类似的步骤 *** 作即可。需要注意的是,ListBox控件并不是最佳的容器类型,如果您需要在WinForm中添加更复杂的控件,可以考虑使用Panel或者TabControl等容器控件。
package com.billy.demoimport java.util.ArrayList
import java.util.List
import android.app.ListActivity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.TextView
import android.widget.CompoundButton.OnCheckedChangeListener
public class TestListViewAndCheckbox extends ListActivity {
/** Called when the activity is first created. */
Context context = null
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
context = getApplicationContext()
setListAdapter(new MyListAdapter())
}
class MyListAdapter extends BaseAdapter{
String data[] = new String[]{"apple", "pear", "banana", "orange","apple", "pear", "banana", "orange","apple", "pear", "banana", "orange"}
List<Integer>checkPosition = new ArrayList<Integer>(data.length)
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.length
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data[position]
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (null == convertView){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
convertView = inflater.inflate(R.layout.list_item, parent, false)
}
TextView text = (TextView)convertView.findViewById(R.id.info)
final CheckBox checkbox = (CheckBox)convertView.findViewById(R.id.checkstatus)
checkbox.setTag(new Integer(position))
text.setText(data[position])
if (checkPosition != null){
checkbox.setChecked((checkPosition.contains(new Integer(position)) ? true : false))
}else{
checkbox.setChecked(false)
}
checkbox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked){
if (!checkPosition.contains(checkbox.getTag())){
checkPosition.add((Integer)checkbox.getTag())
}
}else{
if (checkPosition.contains(checkbox.getTag())){
checkPosition.remove(checkbox.getTag())
}
}
}
})
return convertView
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)