人品计算器demo

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

下面是代码: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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值