| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4544 人关注过本帖
标题:(安卓)一点击按钮就崩掉了..求助!谢谢!
取消只看楼主 加入收藏
菜鸟要努力
Rank: 1
等 级:新手上路
帖 子:27
专家分:1
注 册:2013-3-4
结帖率:100%
收藏
已结贴  问题点数:19 回复次数:2 
(安卓)一点击按钮就崩掉了..求助!谢谢!
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);
              }   
        }
    });
    }
}
搜索更多相关主题的帖子: import public package 
2015-04-06 18:34
菜鸟要努力
Rank: 1
等 级:新手上路
帖 子:27
专家分:1
注 册:2013-3-4
收藏
得分:0 
我又改了一下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);
              }   
        }
    });
    }
}
2015-04-06 18:50
菜鸟要努力
Rank: 1
等 级:新手上路
帖 子:27
专家分:1
注 册:2013-3-4
收藏
得分:0 
回复 2楼 tlliqi
我又改了一下,希望能表达清楚TAT..
就是要做一个可以选择Radiobutton的性别以及输入edittext的身高 然后计算出体重这样的东西
本来还没有添加addTextChangedListener的时候还可以用
但是一限制身高是100.0—200.0的区间范围 添加了addTextChangedListener就一按按钮就崩溃了...
2015-04-06 18:54
快速回复:(安卓)一点击按钮就崩掉了..求助!谢谢!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039139 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved