安卓开发中的基本组件(二)
2014-10-25 10:34
190 查看
1. 单选按钮
package org.lxh.demo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MyRadioListenerDemo extends Activity {
private TextView show = null ;
private RadioGroup sex = null ;// 取得单选钮
private RadioButton male = null ;
private RadioButton female = null ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.show = (TextView) super.findViewById(R.id.show) ;// 取得文本框
this.sex = (RadioGroup) super.findViewById(R.id.sex) ;// 取得单选钮选项
this.male = (RadioButton) super.findViewById(R.id.male) ;
this.female = (RadioButton) super.findViewById(R.id.female) ;
this.sex.setOnCheckedChangeListener(new OnCheckedChangeListenerImpl()) ;
}
private class OnCheckedChangeListenerImpl implements
OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
String temp = null ;
if (MyRadioListenerDemo.this.male.getId() == checkedId) {
temp = MyRadioListenerDemo.this.male
.getText().toString();
// 取得单选钮文本
}
if (MyRadioListenerDemo.this.female.getId() == checkedId) {
temp = MyRadioListenerDemo.this.female
.getText().toString();// 取得单选钮文本
}
MyRadioListenerDemo.this.show
.setText("您的性别是:" + temp);// 设置文本显示
}
}
}
2. 下拉列表框 OnItemSelectedListener
private class OnItemSelectedListenerImpl implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {// 选项选中时触发
String value = adapterView.getItemAtPosition(position).toString();// 取得选项内容
MySpinnerListenerDemo.this.info.setText("您喜欢的城市是:" + value);
}
上面的程序中
AdapterView<?> adapterView, java泛型
?是通配符
3. 焦点事件
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
this.edit.setOnFocusChangeListener(new OnFocusChangeListenerImpl());// 设置焦点事件
this.edit.setOnClickListener(new OnClickListenerImpl()) ;// 设置单击事件
private class OnFocusChangeListenerImpl implements OnFocusChangeListener
4. 长按事件
this.img.setOnLongClickListener(new OnLongClickListenerImpl()); // 定义长按监听
private class OnLongClickListenerImpl implements OnLongClickListener
5. ScrollView
继承于 FrameLayout,作为一个能滚动内容的容器。一般包含一个组件 LinerLayout,再由LinerLayout包含多个组件。
6。 ListView
package org.lxh.demo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MyRadioListenerDemo extends Activity {
private TextView show = null ;
private RadioGroup sex = null ;// 取得单选钮
private RadioButton male = null ;
private RadioButton female = null ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.show = (TextView) super.findViewById(R.id.show) ;// 取得文本框
this.sex = (RadioGroup) super.findViewById(R.id.sex) ;// 取得单选钮选项
this.male = (RadioButton) super.findViewById(R.id.male) ;
this.female = (RadioButton) super.findViewById(R.id.female) ;
this.sex.setOnCheckedChangeListener(new OnCheckedChangeListenerImpl()) ;
}
private class OnCheckedChangeListenerImpl implements
OnCheckedChangeListener {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
String temp = null ;
if (MyRadioListenerDemo.this.male.getId() == checkedId) {
temp = MyRadioListenerDemo.this.male
.getText().toString();
// 取得单选钮文本
}
if (MyRadioListenerDemo.this.female.getId() == checkedId) {
temp = MyRadioListenerDemo.this.female
.getText().toString();// 取得单选钮文本
}
MyRadioListenerDemo.this.show
.setText("您的性别是:" + temp);// 设置文本显示
}
}
}
2. 下拉列表框 OnItemSelectedListener
private class OnItemSelectedListenerImpl implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {// 选项选中时触发
String value = adapterView.getItemAtPosition(position).toString();// 取得选项内容
MySpinnerListenerDemo.this.info.setText("您喜欢的城市是:" + value);
}
上面的程序中
AdapterView<?> adapterView, java泛型
?是通配符
3. 焦点事件
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
this.edit.setOnFocusChangeListener(new OnFocusChangeListenerImpl());// 设置焦点事件
this.edit.setOnClickListener(new OnClickListenerImpl()) ;// 设置单击事件
private class OnFocusChangeListenerImpl implements OnFocusChangeListener
4. 长按事件
this.img.setOnLongClickListener(new OnLongClickListenerImpl()); // 定义长按监听
private class OnLongClickListenerImpl implements OnLongClickListener
5. ScrollView
继承于 FrameLayout,作为一个能滚动内容的容器。一般包含一个组件 LinerLayout,再由LinerLayout包含多个组件。
6。 ListView
相关文章推荐
- 安卓开发中的基本组件(一)
- 【XFeng安卓开发笔记】四大基本组件——跨应用启动service
- Android开发之基本组件学习-----------TextView的使用
- Android开发之基本组件学习----------EditView组件
- 安卓开发笔记——Gallery组件+ImageSwitcher组件
- 安卓开发笔记——Fragment+FragmentTabHost组件(实现新浪微博底部菜单)
- IOS 开发--常用基本组件
- 手机探索者开发实录—基本组件
- 安卓开发笔记——WebView组件
- android开发3:四大基本组件的介绍与生命周期
- 安卓开发笔记——Fragment+ViewPager组件(高仿微信界面)
- 【安卓深度控件开发(2.1)】LCDView - 基本绘图
- 安卓重要组件#5-ListView控件一些基本属性,添加头尾布局和小结
- 安卓重要组件#1--ListView创建及基本的使用方法
- 安卓开发笔记——GridView组件
- Android开发 (一) 基本组件整理
- android开发3:四大基本组件的介绍与生命周期
- 安卓开发日记1——基本环境搭建
- Android开发之基本组件学习--------Button组件
- 安卓开发复习笔记——ViewPager组件(仿微信引导界面)