导航

 

一、事件三要素:

                         事件源:             事件发生的来源

                         事件:                  行为(点击、触摸…)

                         事件监听器:当事件发生时,所要做的事情

二、OnClickListener (单击事件)

         接口定义: Public static interface View . OnClickListener {

                                                                         Public void OnClick (View view);

                                          }

protected void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);

                setContentView(R.layout.focus);

                Button bt=(Button)super.findViewById(R.id.focusbt1);

                //设置监听器——>事件处理(View.OnClickListener)

                OnClickListener ocl=new OnClickListenerImpl();

                bt.setOnClickListener(ocl);    

}

//内部类

public class OnClickListenerImpl implements OnClickListener{

                public void onClick(View v) {

                                String str=etname.getText().toString();

                                tv.setText(str);

                }

}

  或则:

Button bt=(Button)super.findViewById(R.id.focusbt1);

 //设置监听器,匿名内部类

 bt.setOnClickListener(new OnClickListener () {

                public void onClick(View v) {

                                String str=etname.getText().toString();

                                tv.setText(str);

                }

  });         

OnCheckedChangeListener【单选发生变化】(处理用户在选中单选按钮的选项时触发

RadioGroup rg = ( RadioGroup) findById(new OnCheckedChangeListener(){

                Public void OnCheckedChanged (RadioGroup group, int checked){

                                ……

                }

});

  

四、 OnItemSelectedListener(下拉列表监听器)

1.获得一个下拉列表控件

                Spinner spin=(Spinner)findById(R. id. xml文件中Spinner的id);

2.定义一个下拉列表内容适配器

                ArrayAdapter<CharSequence> adapter= ArrayAdapter.createFromResource ();

                // Adapter . setDropDownViewResource ();//设置列表风格

3.将适配器对象传入spin

                spin.setAdapter(adapter);

// 读取下拉框选中内容

                String text=下拉列表对象名.getSelectedItem().toString();

  

级联下拉列表的实现

                                // 定义适配器,读取xml文件中省份的内容

                                ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(

                                                                this, R.array.pro, android.R.layout.simple_spinner_item);

                                // R.array.pro——string.xml中名称为pro的string-array

                                //设置列表显示风格

                                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                                //第一个下拉框

                                final Spinner spin = (Spinner) super.findViewById(R.id.Spinner01);

                                spin.setAdapter(adapter);               //将省份的适配器对象传入

                                spin.setPrompt("选择省份");

 

                                // 定义适配器,读取xml文件中湖南省城市的内容

                                final ArrayAdapter<CharSequence> c1 = ArrayAdapter.createFromResource(

                                                                this, R.array.HuNancity, android.R.layout.simple_spinner_item);

                                // 定义适配器,读取xml文件中广东省城市的内容

                                final ArrayAdapter<CharSequence> c2 = ArrayAdapter.createFromResource(

                                                                this, R.array.guangdongcity,

                                                                android.R.layout.simple_spinner_item);

 

                                spin.setOnItemSelectedListener(new OnItemSelectedListener() {

                                                public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) {

                                                                final Spinner sp1 = (Spinner) findViewById(R.id.Spinner02);

                                                                // 读取【省份名称】的下拉框选中内容

                                                                final String itemPro = spin.getSelectedItem().toString();

                                                                if (itemPro.equals("湖南省")) {

                                                                                sp1.setAdapter(c1);

                                                                } else if (itemPro.equals("广东省")) {

                                                                                sp1.setAdapter(c2);

                                                                }

                                                }

                                                public void onNothingSelected(AdapterView<?> arg0) {}

                                });

五、 OnTimeChangedListener(日期和时间的时间处理)

         DatePicker

    

/*取得DatePicker对象,以init()设置初始值与OnDateChangedListener()*/

DatePicker dp= (DatePicker)findViewById(R.id.DatePicker01);

dp.init (year, month, day, new OnDateChangedListener () {

                public void onDateChanged(DatePicker view,int Year,int monthOfYear,int dayOfMonth) {}

});

TimePicker

/*取得TimePicker对象,并设置为24小时制*/

TimePicker tp= (TimePicker)findViewById(R.id.TimePicker01);

tp.setIs24HourView (true);

tp.setOnTimeChangedListener (new OnTimeChangedListener () {

                public void onTimeChanged(TimePicker view, int hourOfDay, int Minute) {}

});

posted on 2017-05-26 15:28  难以忘记的  阅读(101)  评论(0编辑  收藏  举报