android部分控件如何获取到输入的值
2015-12-24 22:03
441 查看
package com.haha.day03layouthomework; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.Spinner; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener, OnCheckedChangeListener, android.widget.CompoundButton.OnCheckedChangeListener, OnItemSelectedListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.confirm_register).setOnClickListener(this); RadioGroup _RadioGroup=(RadioGroup) findViewById(R.id.hobby_radio); _RadioGroup.setOnCheckedChangeListener(this); Spinner _Spinner=(Spinner) findViewById(R.id.spinner_one); // 建立数据源 String[] mItems = getResources().getStringArray(R.array.hobby_a); // 建立Adapter并且绑定数据源 ArrayAdapter _Adapter=new ArrayAdapter(this,android.R.layout.simple_spinner_item, mItems); //绑定 Adapter到控件 _Spinner.setAdapter(_Adapter); _Spinner.setOnItemSelectedListener(this); //checkbox CheckBox watch_moive=(CheckBox) findViewById(R.id.watch_movie); watch_moive.setOnCheckedChangeListener(this); CheckBox play_foooball=(CheckBox) findViewById(R.id.play_football); play_foooball.setOnCheckedChangeListener(this); CheckBox go_mountain=(CheckBox) findViewById(R.id.go_mountain); go_mountain.setOnCheckedChangeListener(this); CheckBox play_game=(CheckBox) findViewById(R.id.play_game); play_game.setOnCheckedChangeListener(this); findViewById(R.id.btn_all).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub CheckBox watch_moive=(CheckBox) findViewById(R.id.watch_movie); CheckBox play_foooball=(CheckBox) findViewById(R.id.play_football); CheckBox go_mountain=(CheckBox) findViewById(R.id.go_mountain); CheckBox play_game=(CheckBox) findViewById(R.id.play_game); watch_moive.setChecked(true); play_foooball.setChecked(true); go_mountain.setChecked(true); play_game.setChecked(true); } }); findViewById(R.id.btn_disall).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub CheckBox watch_moive=(CheckBox) findViewById(R.id.watch_movie); CheckBox play_foooball=(CheckBox) findViewById(R.id.play_football); CheckBox go_mountain=(CheckBox) findViewById(R.id.go_mountain); CheckBox play_game=(CheckBox) findViewById(R.id.play_game); if(watch_moive.isChecked()){ watch_moive.setChecked(false); }else { watch_moive.setChecked(true); } if(play_foooball.isChecked()){ play_foooball.setChecked(false); }else { play_foooball.setChecked(true); } if(go_mountain.isChecked()){ go_mountain.setChecked(false); }else { go_mountain.setChecked(true); } if(play_game.isChecked()){ play_game.setChecked(false); }else { play_game.setChecked(true); } } }); } @Override public void onClick(View v) { // TODO Auto-generated method stub EditText _EditTextName=(EditText) findViewById(R.id.edit_username); String _editTextName=_EditTextName.getText().toString(); EditText _EditTextPassword=(EditText) findViewById(R.id.edit_password); String _editTextPassword=_EditTextPassword.getText().toString(); if(_editTextName!=null&&_editTextPassword!=null){ Toast.makeText(this, "用户名="+_editTextName+";密码="+_editTextPassword, Toast.LENGTH_LONG).show(); } } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub if(checkedId==R.id.hobby_radio1){ RadioButton _RadioButton1=(RadioButton) findViewById(R.id.hobby_radio1); String _radioButton1=(String) _RadioButton1.getText(); Toast.makeText(this, _radioButton1, Toast.LENGTH_LONG).show(); }else if(checkedId==R.id.hobby_radio2){ RadioButton _RadioButton2=(RadioButton) findViewById(R.id.hobby_radio2); String _radioButton2=(String) _RadioButton2.getText(); Toast.makeText(this, _radioButton2, Toast.LENGTH_LONG).show(); } } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked){ String a=(String) buttonView.getText(); // String a=buttonView.toString(); Toast.makeText(this, a, Toast.LENGTH_LONG).show(); } } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub String str=parent.getItemAtPosition(position).toString(); Toast.makeText(this, "你的职业是:"+str, 2000).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }
相关文章推荐
- Android XML解析器 - PUll
- 使用AndroidAnnotations框架优雅地实现ListView功能例子
- Android上使用OpenCV处理图像
- Android 人名 随机抽奖
- Android-Creating an Input Method(IME)
- 练手Android应用
- Android Activity页面加载时间性能分析,以及改进要点
- How to add a new key on android
- 读Android学Java基础之按位操作符
- Android自助餐之文件存储
- 事件监听:诀别Android繁琐的事件注册机制——view.setOnXXXXListener
- Android Studio上手,基于VideoView的本地文件及流媒体播放器
- Android属性动画深入分析:让你成为动画牛人
- Android源码分析—属性动画的工作原理
- Android实战之 Jmail发送邮件(无需跳转到邮件客户端)
- Android源码分析-全面理解Context
- Android Studio 安装配置问题总结
- Android 数据存储之SharedPreference
- Android中屏幕密度和图片大小的关系分析
- Android源码分析—属性动画的工作原理