人品计算器demo

来源:互联网 发布:手机淘宝开店的流程图 编辑:程序博客网 时间:2024/05/22 04:27

最近在玩小游戏的时候看到人品计算器 感觉不难做就试了下,当然做的很简陋~~

下面是代码:MainActivity 获取姓名,性别传到下个界面去

public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private EditText et_name;    private RadioGroup rg_sex;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        et_name = (EditText) findViewById(R.id.et_name);        rg_sex = (RadioGroup) findViewById(R.id.rg_sex);        Button bt_calcu = (Button) findViewById(R.id.bt_calcu);        assert bt_calcu != null;        bt_calcu.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.bt_calcu:                String name = et_name.getText().toString().trim();                if (TextUtils.isEmpty(name)) {                    Toast.makeText(this, "姓名不能为空", Toast.LENGTH_SHORT).show();                    return;                }                int checkedRadioButtonId = rg_sex.getCheckedRadioButtonId();                int sex = 0;                switch (checkedRadioButtonId) {                    case R.id.rb_man:                        sex = 1;                        break;                    case R.id.rb_woman:                        sex = 2;                        break;                    case R.id.rb_taijian:                        sex = 3;                        break;                }                if (sex == 0) {                    Toast.makeText(this, "请选择性别", Toast.LENGTH_SHORT).show();                    return;                }                Intent intent = new Intent(this, ResultActivity.class);                intent.putExtra("name", name);                intent.putExtra("sex", sex);                startActivity(intent);                break;        }    }}

这是布局代码

<EditText        android:id="@+id/et_name"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入您的姓名" />    <RadioGroup        android:id="@+id/rg_sex"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <RadioButton            android:id="@+id/rb_man"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="男"            />        <RadioButton            android:id="@+id/rb_woman"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="50dp"            android:layout_marginRight="50dp"            android:text="女" />        <RadioButton            android:id="@+id/rb_taijian"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="人妖" />    </RadioGroup>    <Button        android:id="@+id/bt_calcu"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="计算"        android:textSize="25sp" />

ResultActivity :显示测试结果,人品是随机数决定的(这样最简单:))

public class ResultActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_result);        TextView tv_name = (TextView) findViewById(R.id.tv_name);        TextView tv_sex = (TextView) findViewById(R.id.tv_sex);        TextView tv_rp = (TextView) findViewById(R.id.tv_rp);        Intent intent = getIntent();        String name = intent.getStringExtra("name");        int sex = intent.getIntExtra("sex", 0);        tv_name.setText("姓名:" + name);        switch (sex) {            case 1:                tv_sex.setText("性别:男");                break;            case 2:                tv_sex.setText("性别:女");                break;            case 3:                tv_sex.setText("性别:太监");                break;        }        int random = (int) (Math.random() * 10);        switch (random) {            case 0:                tv_rp.setText("您的人品很差!!!");                break;            case 1:            case 2:            case 3:                tv_rp.setText("您的人品一般般哦!");                break;            case 4:            case 5:            case 6:                tv_rp.setText("您的人品还可以哦!");                break;            case 7:            case 8:            case 9:                tv_rp.setText("您的人品非常棒哦!");                break;            default:        }    }}

布局代码

    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="姓名:张三"        android:textSize="25sp"        android:id="@+id/tv_name"        />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="性别:男"        android:textSize="25sp"        android:id="@+id/tv_sex"        />    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="您的人品非常好!"        android:textSize="25sp"        android:id="@+id/tv_rp"        />

写完测试的时候一直崩溃,半天都找不到错误,做后才发现是ResultActivity没注册~

0 0
原创粉丝点击