1
我正在写一个应用程序,如列表视图与切换按钮。 这是我的源代码。列表视图与切换按钮
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),R.layout.main,R.id.textView1,getResources().getStringArray(R.array.Strings));
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item+" selected", Toast.LENGTH_LONG).show();
}
而且我从进口字符串资源列表,我的sring.xml是 MyListViewWithToggleButton 世界,你好! 设置 MyListViewWithToggle ON/OFF
<string-array name="Strings">
<item >Relay1</item>
<item >Relay2</item>
<item >Relay3</item>
<item >Relay4</item>
<item >Relay5</item>
<item >Relay6</item>
<item >Relay7</item>
<item >Relay8</item>
</string-array>
</resources>
而在亩main.xml中我使用一个文本视图和切换按钮
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/toggleButton1"
android:textSize="20dp" >
</TextView>
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:text="@string/On_Off" >
</ToggleButton>
</RelativeLayout>
当我点击切换按钮它示出开和关。 但是,当我点击列表,我没有得到任何答复。
对于我的应用程序,我需要发送一些数据,同时按下切换按钮的按钮条件。
海感谢您的回复.........所以我需要改变我xml文件也.....也可以使用同一个吗? – Haris
谢谢......我会试试这个代码...... – Haris