Android 开发 列表选择框实现<列表项可知>
2015-07-16 11:10
459 查看
将列表选择框的内容以数组形式写进xml,存在value中
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ctype">
<item>ch1</item>
<item>ch2</item>
<item>ch3</item>
<item>ch4</item>
<item>ch5</item>
</string-array>
</resources>
在layout中设置Spinner
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="choose"
android:height="50px"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ch"
android:entries="@array/ctype">
</Spinner>
</LinearLayout>
在MainActivity中通过id找到组件,并设置监听事件
package com.p3_3;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.ch);
spinner.getSelectedItem();
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String result = adapterView.getItemAtPosition(i).toString();
Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="ctype">
<item>ch1</item>
<item>ch2</item>
<item>ch3</item>
<item>ch4</item>
<item>ch5</item>
</string-array>
</resources>
在layout中设置Spinner
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="choose"
android:height="50px"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ch"
android:entries="@array/ctype">
</Spinner>
</LinearLayout>
在MainActivity中通过id找到组件,并设置监听事件
package com.p3_3;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
public class MainActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.ch);
spinner.getSelectedItem();
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String result = adapterView.getItemAtPosition(i).toString();
Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
相关文章推荐
- Toast 用法注意事项
- Android5.x新特性之RecyclerGridView
- Android LayoutInflater详解
- Android Menu
- Android studio 导入github工程
- Android Fragment 基本介绍
- 2015年 Android 开发的新技术
- Android所有系统资源图标android.R.drawable查看
- Android基础:listview的item点击事件会使里面的Button也出现按压的效果
- Android系统五大布局详解Layout
- Android音频系统探究——从SoundPool到AudioHardware
- android network develop(3)----Xml Parser
- VS源码文件导入Android工程 中文乱码
- Android学习之.9.png图片处理和控件美化shape
- [Android] ImageView.ScaleType设置图解
- Android - View Alpha值
- Webservice 封装为 Thread 在主线程进行调用--Android
- Android 下拉刷新框架实现
- android 调用 Menu 两种方法
- Android中使用4种方式实现按钮点击操作