安卓开发中的基本组件(二)


  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






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值