public
class
MainActivity
extends
Activity {
private
EditText
ed_name
;
private
ProgressBar
pb
;
private
TextView
tv
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main
);
//拿到控件
ed_name
=(EditText) findViewById(R.id.
et_name
);
pb
=(ProgressBar) findViewById(R.id.
pb
);
tv
=(TextView) findViewById(R.id.
tv
);
}
/**
* Intent传递数据
*/
public
void
click(View view){
tv
.setVisibility(View.
VISIBLE
) ;
//方法调用时tv可见
final
String name =
ed_name
.getText().toString().trim();
//得到姓名
new
Thread(){
public
void
run() {
byte
[] results = name.getBytes();
int
sum=0;
pb
.setMax(results.
length
);
//设置进度条的最大值
for
(
int
i=0;i<results.
length
;i++){
int
number=(
int
)results[i];
sum=sum+number;
try
{
Thread. sleep(300);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
pb
.setProgress(i);
//设置进度条变化
}
int
rp=Math.abs (sum%100);
//创建意图开启一个新的界面,并传递数据
Intent intent=
new
Intent(getApplicationContext(),RPActivity.
class
);
intent.putExtra(
"name"
,
"HELLO"
);
intent.putExtra(
"rp"
, 123);
startActivity(intent);
};
}.start();
}
--------------------------------------------------------------------------------------------------
public
class
RPActivity
extends
Activity {
private
TextView
tv_info
;
private
TextView
result
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.
rp
);
//拿到控件
tv_info
=(TextView) findViewById(R.id.
tv_info
);
result
=(TextView) findViewById(R.id.
tv_result
);
//创建意图
Intent intent=
new
Intent();
String name=intent.getStringExtra(
"name"
);
//接收传递过来的参数
int
rp=intent.getIntExtra(
"rp"
, 0);
tv_info
.setText(name+
"的人品是: "
);
if
(rp<50){
tv_info
.setTextColor(Color.
GREEN
);
tv_info
.setText(
"计算的人品是:"
+rp);
}
else
{
tv_info
.setTextColor(Color.
BLUE
);
tv_info
.setText(
"计算的人品是:"
+rp);
}
}
}