具体例题在360云盘中http://yunpan.cn/cZNcQw3cf3Spb 访问密码 9d90
清单文件同样需要注册Activity
xml文件
<
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"
>
<
TextView
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_gravity
=
"center_horizontal"
android:textSize
=
"28sp"
android:textColor
=
"#ff0000"
android:text
=
"人品测试"
/>
<
EditText
android:id
=
"@+id/et_name"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:hint
=
"请输入姓名:"
/>
<
Button
android:id
=
"@+id/test"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:text
=
"开始测试"
/>
</
LinearLayout
>
主Java文件
public
class
MainActivity
extends
Activity
implements
OnClickListener {
private
EditText
et_name
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main
);
Button test = (Button) findViewById(R.id.
test
);
et_name
= (EditText) findViewById(R.id.
et_name
);
test.setOnClickListener(
this
);
}
@Override
public
void
onClick(View v) {
String name=
et_name
.getText().toString().trim();
if
(TextUtils.isEmpty(name)){
new
AlertDialog.Builder(
this
).setMessage(
"名字不能为空"
).show();
}
else
{
Intent intent =
new Intent(this , ResultActivity.class);
intent.putExtra( "name",name);
startActivity(intent);
}
}
}
副xml文件
<?
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:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:textSize
=
"28sp"
android:textColor
=
"#ff0000"
android:text
=
"你的人品值"
android:layout_gravity
=
"center_horizontal"
/>
<
TextView
android:id
=
"@+id/tv_result"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
/>
<
ProgressBar
android:id
=
"@+id/pb_testRP"
style
=
"?android:attr/progressBarStyleHorizontal"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:max="100"
/>
</
LinearLayout
>
副Java文件
public
class
ResultActivity
extends
Activity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
//
TODO
Auto-generated method stub
super
.onCreate(savedInstanceState);
setContentView(R.layout.
result
);
TextView result =(TextView) findViewById(R.id.
tv_result
);
Intent intent = getIntent();
String name= intent.getStringExtra( "name");
Random random =
new
Random();
int
rp = random.nextInt(101);
result.setText(name+
"您的人品值为:"
+rp);
ProgressBar pb = (ProgressBar) findViewById(R.id.
pb_testRP
);
pb.setProgress(rp);
}
}