#2
tlliqi2015-04-06 18:44
|
package st.st.st;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class St2 extends Activity {
//肥胖测试
public RadioGroup group;
public RadioButton man,woman;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
Button btn1=(Button)findViewById(R.id.button1);//返回主界面按钮
Button btn2=(Button)findViewById(R.id.button2);//计算按钮
//返回主界面按钮
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
//设置Intent对象要启动的Activity
intent.setClass(St2.this,St1Activity.class);
//通过Intent对象启动另外一个Activity
startActivity(intent);
St2.this.finish();
}
});
//计算按钮事件
btn2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
/*取得输入的身高*/
final EditText height=(EditText)findViewById(R.id.height);
double height1=Double.parseDouble(height.getText().toString());
/*限制身高输入范围为100.0至250.0厘米*/
height.addTextChangedListener(new TextWatcher()
{
private double MIN_MARK = 100.0;
private double MAX_MARK = 200.0;
public void afterTextChanged(Editable s)
{
if (s != null && !s.equals(""))
{
if (MIN_MARK != -1 && MAX_MARK != -1)
{
double markVal = 100.0;
try
{
markVal = Integer.parseInt(s.toString());
}
catch (NumberFormatException e)
{
markVal = 100.0;
}
if (markVal > MAX_MARK)
{
Toast.makeText(getBaseContext(), "身高范围为100.0-200.0厘米", Toast.LENGTH_SHORT).show();
height.setText(String.valueOf(MAX_MARK));
}
return;
}
}
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (start > 1)
{
if (MIN_MARK != -1 && MAX_MARK != -1)
{
int num = Integer.parseInt(s.toString());
if (num > MAX_MARK)
{
s = String.valueOf(MAX_MARK);
height.setText(s);
}
else if(num < MIN_MARK)
s = String.valueOf(MIN_MARK);
height.setText(s);
return;
}
}
}
});
/*取得选择的性别*/
String sex="";
group=(RadioGroup)findViewById(R.id.radioGroup);
man=(RadioButton)findViewById(R.id.radio0);
woman=(RadioButton)findViewById(R.id.radio1);
if(man.isChecked()){
woman.setChecked(false);
sex="M";
}
else{
woman.setChecked(true);
sex="F";
}
/*判断身高是否为空*/
if (height.equals("")){
Toast.makeText(getBaseContext(), "请输入完整信息", Toast.LENGTH_SHORT).show();
}
else{
Intent intent=new Intent();
intent.setClass(St2.this, St2_1.class);
Bundle bundle=new Bundle();
bundle.putDouble("height", height1);
bundle.putString("sex", sex);
/*将Bundle对象assign给Intent*/
intent.putExtras(bundle);
/*调用St2_1*/
startActivity(intent);
}
}
});
}
}