Android实例开发项目之人品测试软件(初级)

项目名称:人品测试软件

功能:

  • 点击“测试”按钮跳转页面
  • “姓名栏”不能为空,如果为空要在页面显示,不能跳转
  • 在第二页面调用第一页面“姓名栏”数据,并利用随机数赋值人品值

用到的知识点

  • 常用布局和控件的设置:RlativeLayout布局的运用、Textvie、Button控件的设置
  • intent实现页面的显式跳转,和数据的传递和接收
  • 通过设置布局文件中Button的onclick属性设置点击事件
    在这里插入图片描述在这里插入图片描述
    MainActivity.xml
<RelativeLayout 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"
    tools:context="cn.experiment.charactertest.MainActivity">
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:layout_marginTop="30dp"
        android:gravity="center_horizontal"
        android:text= "人品计算器"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_title"
        android:hint="请输入姓名"
        android:layout_marginTop="20dp"
        android:id="@+id/et_name" />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/et_name">
        <Button
            android:id="@+id/btn_calculate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="计   算"
            android:textSize="30dp"
            android:onClick="click"
            android:gravity="center_horizontal"
            android:layout_centerInParent="true"/>
    </RelativeLayout>
</RelativeLayout>

ResultActivity.xml

<RelativeLayout 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"
    tools:context="cn.experiment.charactertest.ResultActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="测试结果:"
        android:textSize="30dp"
        android:id="@+id/tv_result_title"
        android:layout_marginTop="50dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_result"
        android:layout_below="@+id/tv_result_title"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp" />
</RelativeLayout>

MainActivity .java

public class MainActivity extends AppCompatActivity {
    private TextView et_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_name = (TextView) findViewById(R.id.et_name) ;

    }
    //点击事件的方法
    public void click(View view) {
        //文本框的内容字符串化
        String name = et_name.getText().toString().trim();
        //判断输入是否为空
        if (TextUtils.isEmpty(name)) {
            Toast.makeText(this, "名字不能为空", Toast.LENGTH_SHORT).show();
        } else {
            //intent实现跳转
            Intent intent = new Intent(this, ResultActivity.class);
            //intent进行数据传递
            intent.putExtra("name", name);
            startActivity(intent);
        }
    }
}

ResultActivity .java

public class ResultActivity extends AppCompatActivity {
    private TextView tv_result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        tv_result= (TextView) findViewById(R.id.tv_result);
        //获取到intent传递的数据
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        //定义一个随机函数
        Random random = new Random();
        int number = random.nextInt(101);
        //文本框显示
        tv_result.setText(name+"您的人品值为:"+number);
    }
}

【评论需要解决的问题或者文章中的不恰当的地方,接受改正】

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值