您的位置:首页 > 移动开发 > Android开发

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

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: