注册 登录
编程论坛 Android开发

(安卓)一点击按钮就崩掉了..求助!谢谢!

菜鸟要努力 发布于 2015-04-06 18:34, 4544 次点击
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);
              }   
        }
    });
    }
}
3 回复
#2
tlliqi2015-04-06 18:44
不懂 帮不上
#3
菜鸟要努力2015-04-06 18:50
我又改了一下TAT 希望能表达清楚...
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.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);//计算按钮
        /*取得输入的身高*/   
        final EditText height=(EditText)findViewById(R.id.height);
        //double height1=Double.parseDouble(height.getText().toString());
        /*限制身高输入范围*/
        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;
                    }
                }
          }
        });
        
        
        //返回主界面按钮
    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
            /*取得身高*/
            double height1=Double.parseDouble(height.getText().toString());
            /*取得选择的性别*/
            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);
              }   
        }
    });
    }
}
#4
菜鸟要努力2015-04-06 18:54
回复 2楼 tlliqi
我又改了一下,希望能表达清楚TAT..
就是要做一个可以选择Radiobutton的性别以及输入edittext的身高 然后计算出体重这样的东西
本来还没有添加addTextChangedListener的时候还可以用
但是一限制身高是100.0—200.0的区间范围 添加了addTextChangedListener就一按按钮就崩溃了...
1