博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android学习笔记(一) 使用选择部件
阅读量:6159 次
发布时间:2019-06-21

本文共 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);               ArrayAdapter
aa=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,如需转载请自行联系原作者

你可能感兴趣的文章
nilfs (a continuent snapshot file system) used with PostgreSQL
查看>>
【SICP练习】150 练习4.6
查看>>
HTTP缓存应用
查看>>
KubeEdge向左,K3S向右
查看>>
DTCC2013:基于网络监听数据库安全审计
查看>>
CCNA考试要点大搜集(二)
查看>>
ajax查询数据库时数据无法更新的问题
查看>>
Kickstart 无人职守安装,终于搞定了。
查看>>
linux开源万岁
查看>>
linux/CentOS6忘记root密码解决办法
查看>>
25个常用的Linux iptables规则
查看>>
集中管理系统--puppet
查看>>
Exchange 2013 PowerShell配置文件
查看>>
JavaAPI详解系列(1):String类(1)
查看>>
HTML条件注释判断IE<!--[if IE]><!--[if lt IE 9]>
查看>>
发布和逸出-构造过程中使this引用逸出
查看>>
使用SanLock建立简单的HA服务
查看>>
Subversion使用Redmine帐户验证简单应用、高级应用以及优化
查看>>
Javascript Ajax 异步请求
查看>>
DBCP连接池
查看>>