标签:
在res文件夹下的layout的文件夹的xml文件
这样在xml中我们就声明了两个控件,一个EditText和一个TextView
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:layout_marginTop="24dp" android:ems="10" > <requestFocus /> </EditText> </RelativeLayout>
并且我们将为EditText控件添加事件监听setOnKeyListener,并实现了onKey方法--在这个项目中也就是用户在按键是变回触发这个事件,从而可以通过getText()方法来获取用户输入的内容,当然,用户在没有输入的默认情况下,编辑框EditText默认显示“输入账号”,实现这个功能的方法为Edit.SetHint(“请输入账号!”)或者在xml布局上写上“android:hit=”请输入账号!””,
代码为
package com.example.test; import android.os.Bundle; import android.app.Activity; import android.view.KeyEvent; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private TextView m_texTextView; private EditText m_EditText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); m_texTextView=(TextView)findViewById(R.id.textView1); m_EditText=(EditText)findViewById(R.id.editText1); m_EditText.setHint("请输入账号:"); m_EditText.setOnKeyListener(new EditText.OnKeyListener(){ @Override public boolean onKey(View arg0,int arg1,KeyEvent arg2){ m_texTextView.setText("文本框中的内容是:"+m_EditText.getText().toString()); return false; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
1.在Activity中自定数据的绑定方式
private static final String[] m={"A型","B型","O型","AB型","其他"}; private ArrayAdapter<String> adapter;
在Oncrete方法中做各种实例化spinner
setContentView(R.layout.activity_main); Spinner spinner = (Spinner) findViewById(R.id.planets_spinner); adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new SpinnerSelectedListener()); spinner.setVisibility(View.VISIBLE);
实现事件的注册
class SpinnerSelectedListener implements OnItemSelectedListener{ @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "你的血型是:"+m[arg2], 1).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }
2.在vlues文件夹中定义arrays.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="plantes"> <item>NOKIA</item> <item>MOTO</item> <item>HTC</item> <item>LG</item> <item>其他</item> </string-array> </resources>
private ArrayAdapter adapter2; rotected void onCreate(Bundle savedInstanceState) { adapter2 = ArrayAdapter.createFromResource(this, R.array.plantes, android.R.layout.simple_spinner_item); adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner1.setAdapter(adapter2); spinner1.setOnItemSelectedListener(new SpinnerXMLSelectedListener()); } class SpinnerXMLSelectedListener implements OnItemSelectedListener{ @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "你使用什么样的手机:"+adapter2.getItem(arg2), 1).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }
注意两种方法分别给adater绑定数据的不同,而且simper_spinner_dropdown_item系统默认的layout方式
public class MainActivity extends Activity { private ListView listView; private ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ListView)this.findViewById(R.id.listView1); adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, getDataSourece()); listView.setAdapter(adapter); } public List<String> getDataSourece() { List<String> list=new ArrayList<String>(); for(int i =0;i<30;i++){ list.add("bibly"+i); } return list; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
1.使用SimpleAdapter+ListView
SimpleAdapter其中一个构造函数如下:
public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
第一个参数:当前上下文对象。第二个参数:一个List类型的泛型集合,泛型类型必须继承之Map类型。第三个:布局资源的ID,第四个参数:需要绑定的Key列表。第五个参数:与Key列表项对应的资源文件中的具体组件ID集合。
自定义一个布局文件image.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/img"> </ImageView> <TextView android:id="@+id/txtName" android:layout_width="wrap_content" android:layout_height="wrap_content" > </TextView> <TextView android:paddingLeft="20sp" android:id="@+id/txtLength" android:layout_width="wrap_content" android:layout_height="wrap_content" > </TextView> </LinearLayout>
package com.example.mytestxml; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.example.mytestxml.R.id; import com.example.mytestxml.R.string; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private ListView listView; private ArrayAdapter<String> adapter; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=(ListView)this.findViewById(R.id.listView1); button=(Button)this.findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String []from={"img","name","length"}; int[]to={R.id.img,R.id.txtName,R.id.txtLength}; SimpleAdapter adapter=new SimpleAdapter(MainActivity.this,getData(), R.layout.image, from, to); listView.setAdapter(adapter); } }); adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, getDataSourece()); listView.setAdapter(adapter); } public List<Map<String, Object>> getData() { List<Map<String,Object>> lists=new ArrayList<Map<String,Object>>(); for(int i=0;i<4;i++){ Map<String,Object> map=new HashMap<String,Object>(); map.put("img", R.drawable.ic_launcher); map.put("name", "SimpleAdapter"+i); map.put("length", "300"); lists.add(map); } return lists; } public List<String> getDataSourece() { List<String> list=new ArrayList<String>(); for(int i =0;i<30;i++){ list.add("bibly"+i); } return list; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
2.使用SimpleAdapter
activity_main.xml下更改代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dip"/> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#16CCDD" android:textSize="22sp" /> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#666666" android:textSize="16sp" /> </LinearLayout> </LinearLayout>
更改MainActivity.java
public class MainActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SimpleAdapter adapter = new SimpleAdapter(this,getData(), R.layout.activity_main, new String[]{"title","info","picture"}, new int[]{R.id.title,R.id.info,R.id.picture}); setListAdapter(adapter); } private List<Map<String, Object>> getData() { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", "图片"); map.put("info", "美辰良景,给你无限的遐思,让人感觉无限温馨……"); map.put("picture", R.drawable.ic_launcher); list.add(map); map = new HashMap<String, Object>(); map.put("title", "音乐"); map.put("info", "轻曼音乐,令人如入仙境,如痴如醉……"); map.put("picture", R.drawable.ic_launcher); list.add(map); map = new HashMap<String, Object>(); map.put("title", "视频"); map.put("info", "震撼场景,360度的视觉捕获,一览无遗……"); map.put("picture", R.drawable.ic_launcher); list.add(map); return list; } }
注意这里的MainActivity extends ListActivity ,ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。
3.使用SimpleAdapter+ListView读取xml数据并访问(使用dom解析的方式读取xml数据)
定义的xml数据
<?xml version="1.0" encoding="utf-8"?> <channel> <item id="0" url="http://www.baidu.com">百度</item> <item id="1" url="http://www.qq.com">腾讯</item> <item id="2" url="http://www.sina.com.cn">新浪</item> <item id="3" url="http://www.taobao.com">淘宝</item> </channel>
定义对应的类来读取数据
public class channel { private String id; private String url; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
定义读取xml读取数据的类(使用Dom方式)
ublic class DomParserHelper { public static List<channel> getChannelList(InputStream stream) { List<channel> list=new ArrayList<channel>(); //得到 DocumentBuilderFactory 对象, 由该对象可以得到 DocumentBuilder 对象 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); try { //得到DocumentBuilder对象 DocumentBuilder builder=factory.newDocumentBuilder(); //得到代表整个xml的Document对象 Document document=builder.parse(stream); //得到 "根节点" Element root=document.getDocumentElement(); //获取根节点的所有items的节点 NodeList items=root.getElementsByTagName("item"); //遍历所有节点 for(int i=0;i<items.getLength();i++) { channel chann=new channel(); Element item=(Element)items.item(i); chann.setId(item.getAttribute("id")); chann.setUrl(item.getAttribute("url")); chann.setName(item.getFirstChild().getNodeValue()); list.add(chann); } } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } }
private List<Map<String, String>> getData2() { List<Map<String, String>> list = new ArrayList<Map<String, String>>(); InputStream stream = getResources().openRawResource(R.raw.channels); List<channel> channlist = DomParserHelper.getChannelList(stream); for (int i = 0; i < channlist.size(); i++) { Map<String, String> map = new HashMap<String, String>(); channel chann = (channel) channlist.get(i); map.put("id", chann.getId()); map.put("url", chann.getUrl()); map.put("name", chann.getName()); list.add(map); } return list; }
button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub String []from={"id","name","url"}; int[]to={R.id.id,R.id.name,R.id.url}; SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,getData2(), R.layout.myxml, from, to); listView.setAdapter(adapter); } });
标签:
原文地址:http://www.cnblogs.com/ilooking/p/4261986.html