標題:
ListView(二)
[打印本頁]
作者:
周政輝
時間:
2018-12-15 10:49
標題:
ListView(二)
試使用 ListView 類別下的 setSelector() 設定選中項的底色,
及 setTextFilterEnabled(true) 開啟項目篩選功能。
[attach]5575[/attach]
作者:
張健勳
時間:
2018-12-15 10:57
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,game)
ListView list = findViewById(R.id.list);
list.setAdapter(adapter);
list.setTextFilterEnabled(true);
list.setSelector(R.color.setSelector);
ListView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
show=findViewById(R.id.show);
show.setText(game[position]);
}
};
list.setOnItemClickListener(listener);
}
複製代碼
作者:
蔡季樺
時間:
2018-12-15 11:04
package com.example.plantsai.myapplicationlistview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private TextView tv;
String data[] = {"AAA","BBB","CCC","DDD","EEE"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_multiple_choice,data);
ListView listView = findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
listView.setSelector(R.color.colorPrimaryDark);
ListView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
tv = findViewById(R.id.textView);
tv.setText(data[position]);
}
};
listView.setOnItemClickListener(listener);
}
}
複製代碼
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/list"
android:layout_width="368dp"
android:layout_height="309dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.033" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.834" />
</android.support.constraint.ConstraintLayout>
複製代碼
作者:
蔡庭豪
時間:
2018-12-15 11:11
package com.example.student.myapplicationlisttest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
String[] menu = {"A","B","C","D"};
ListView listView;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_activated_1,menu);
listView.setAdapter(adapter);
listView.setTextFilterEnabled(true);
listView.setSelector(R.color.Selecetorcolor);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
textView = (TextView) findViewById(R.id.textView);
textView.setText(menu[position]);
}
});
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2