トグルボタン付きのリストビューのようなアプリケーションを書いています。これが私のソースコードです。
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 Hello world!です。設定MyListViewWithToggleON/ 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>
そしてmumain.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>
トグルボタンをクリックすると、オンとオフが表示されます。しかし、リストをクリックしても応答がありません。
私のアプリケーションでは、ボタンの状態に関してトグルボタンを押しながらデータを送信する必要があります。