android完成注册页面的下拉框及单复选框(1)

package com.example.dell.mylogin;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
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.Spinner;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText etName,etPwd;
TextView tvResult;
RadioGroup rg;
RadioButton rbMale,rbFemale;
CheckBox chk1,chk2,chk3;
Spinner spinner;
Button btn;
String sex="", hobby="", city="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);

init();
radioButton();
checkBox();
spinner();
btn.setOnClickListener(this);
}
//城市下拉框
private void spinner() {
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
city = (String) spinner.getSelectedItem();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
}
//单选框
private void radioButton() {
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int i) {
if(i== R.id.rbMale){
sex="男生";
}
else
{
sex="女生";
}
}
});
}
//复选框
private void checkBox() {
chk1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
hobby+= chk1.getText().toString()+" ";
}

}
});
chk2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
hobby+= chk2.getText().toString()+" ";
}

}
});
chk3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
hobby+= chk3.getText().toString()+" ";
}
}
});
}

private void init() {
etName=findViewById(R.id.etName);
etPwd= findViewById(R.id.etPwd);
tvResult= findViewById(R.id.tvResult);
rg=findViewById(R.id.rg);
rbMale= findViewById(R.id.rbMale);
rbFemale=findViewById(R.id.rbFemale);
chk1=findViewById(R.id.chk1);
chk2=findViewById(R.id.chk2);
chk3=findViewById(R.id.chk3);
spinner=findViewById(R.id.spinner);
btn=findViewById(R.id.btn);
}
public void onClick(View view) {
//显示在本页面
// String s="用户名:"+etName.getText().toString()+"\n"
// + "密码:"+etPwd.getText().toString()+"\n"
// +"性别:"+sex+"\n"
// +"爱好:"+hobby+"\n"
// + "居住城市:"+city;
// tvResult.setText(s);

//跳转页面MYActivity
Intent intent=new Intent(MainActivity.this,MyActivity.class);
intent.putExtra("name",etName.getText().toString());
intent.putExtra("pwd",etPwd.getText().toString());
setResult(1,intent);
finish();
}
}

猜你喜欢

转载自www.cnblogs.com/wxyryx/p/9822887.html