本文共 6125 字,大约阅读时间需要 20 分钟。
ListActivity的使用
extend Activity
package com.example.listtest;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.TextView;public class MainActivity extends Activity { TextView selection; ListView list; String[] items={"t0","t1","t2","t3","t4"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,items)); selection=(TextView)findViewById(R.id.textView1); list=(ListView)findViewById(R.id.listView1); //绑定数据 list.setAdapter(new ArrayAdapter (this,android.R.layout.simple_list_item_1,items)); list.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView< ?> arg0, View arg1, int position,long id){ selection.setText(items[position]); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
extend ListActivity
spinner的使用
package com.example.test2;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.ArrayAdapter;import android.widget.Spinner;import android.widget.TextView;public class MainActivity extends Activity { TextView text; Spinner sp; private String[] items={"t0","t1","t2","t3","t4"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text=(TextView)findViewById(R.id.textView1); sp=(Spinner)findViewById(R.id.spinner1); ArrayAdapteraa=new ArrayAdapter (this, android.R.layout.simple_spinner_item,items); aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp.setAdapter(aa); sp.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView< ?> parent, View v, int position, long id){ text.setText(items[position]); } public void onNothingSelected(AdapterView arg0) { // TODO Auto-generated method stub text.setText("Nothing selected!"); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; }}
GridView的使用
package com.example.test3;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.GridView;import android.widget.TextView;public class MainActivity extends Activity { TextView selection; GridView grid; String[] items={"t0","t1","t2","t3","t4","t5","t6","7t","t8","t9","t10","t11","t12","t13","t14","t15","t16","t17","t18","t19",}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); selection=(TextView)findViewById(R.id.textView1); grid=(GridView)findViewById(R.id.gridView1); grid.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1,items)); grid.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub selection.setText(items[arg2]); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; }}
AutoCompleteTextView
package com.example.test5;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.MenuItem;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;import android.widget.TextView;import android.support.v4.app.NavUtils;import android.text.Editable;import android.text.TextWatcher;public class MainActivity extends Activity implements TextWatcher{ TextView selection; AutoCompleteTextView edit; String[] items={"teacher","core","popular","wherese","compromise","teeth"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); selection=(TextView)findViewById(R.id.textView1); edit=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1); edit.addTextChangedListener(this); edit.setAdapter(new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,items)); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub selection.setText(edit.getText()); } }
本文转自cococo点点博客园博客,原文链接:http://www.cnblogs.com/coder2012/archive/2013/05/09/3068353.html,如需转载请自行联系原作者