项目名称:人品测试软件
功能:
- 点击“测试”按钮跳转页面
- “姓名栏”不能为空,如果为空要在页面显示,不能跳转
- 在第二页面调用第一页面“姓名栏”数据,并利用随机数赋值人品值
用到的知识点
- 常用布局和控件的设置: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);
}
}
【评论需要解决的问题或者文章中的不恰当的地方,接受改正】