public class MainActivity extends ActionBarActivity {
private EditText et_name;
private RadioGroup rg_group;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText) findViewById(R.id.et_name);
rg_group = (RadioGroup) findViewById(R.id.radioGroup1);
}
public void click(View v){
String name=et_name.getText().toString().trim();
if(TextUtils.isEmpty(name)){
Toast.makeText(getApplicationContext(), "请输入姓名",1).show();
return;
}
int radioButtonId = rg_group.getCheckedRadioButtonId();
int sex=0;
switch(radioButtonId){
case R.id.radio0:
sex=1;
break;
case R.id.radio1:
sex=2;
break;
case R.id.radio2:
sex=3;
break;
default:
break;
}
if(sex==0){
Toast.makeText(getApplicationContext(), "请选择性别",1).show();
return;
}
Intent intent=new Intent(this,NextActivity.class);
intent.putExtra("name",name);
intent.putExtra("sex",sex);
startActivity(intent);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.calc.MainActivity" >
<EditText
android:id="@+id/et_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入姓名"
/>
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="人妖" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="计算"
android:onClick="click"
/>
</LinearLayout>
public class NextActivity extends Activity {
private TextView name;
private TextView sex;
private TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
name = (TextView) findViewById(R.id.textView1);
sex = (TextView) findViewById(R.id.textView2);
result = (TextView) findViewById(R.id.textView3);
Intent intent=getIntent();
String et_name=intent.getStringExtra("name");
int et_sex=intent.getIntExtra("sex",0);
name.setText(et_name);
switch (et_sex) {
case 1:
sex.setText("男");
break;
case 2:
sex.setText("女");
break;
case 3:
sex.setText("人妖");
break;
default:
break;
}
byte[] bytes=et_name.getBytes();
int total=0;
for(byte b:bytes){
int number=b&0xff;
total+=number;
}
int score=Math.abs(total)%100;
if(score>90){
result.setText("人品好");
}else if(score>80){
result.setText("人品还可以");
}else if (score>60){
result.setText("人品及格");
}else{
result.setText("人品太次");
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您的人品很好" />
</LinearLayout>