亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

用手機調試,根本打不開,點一下就閃退怎么回事啊?

package?com.example1.jisuanqi;

import?android.app.Activity;
import?android.app.SearchManager.OnCancelListener;
import?android.os.Bundle;
import?android.view.Menu;
import?android.view.MenuItem;
import?android.view.View;
import?android.view.View.OnClickListener;
import?android.widget.Button;
import?android.widget.EditText;

public?class?MainActivity?extends?Activity?implements?OnCancelListener{
	Button?btn_0;//0數字按鈕
	Button?btn_1;//1數字按鈕
	Button?btn_2;//2數字按鈕
	Button?btn_3;//3數字按鈕
	Button?btn_4;//4數字按鈕
	Button?btn_5;//5數字按鈕
	Button?btn_6;//6數字按鈕
	Button?btn_7;//7數字按鈕
	Button?btn_8;//8數字按鈕
	Button?btn_9;//9數字按鈕
	Button?btn_point;//點按鈕
	Button?btn_clear;//清除按鈕
	Button?btn_del;//刪除按鈕
	Button?btn_plus;//加按鈕
	Button?btn_minus;//減按鈕
	Button?btn_multiply;//乘按鈕
	Button?btn_divide;//除按鈕
	Button?btn_equal;//等按鈕
	EditText?et_input;//顯示內容的顯示屏
	boolean?clear_flag;//清空標示
	@Override
	protected?void?onCreate(Bundle?savedInstanceState)?{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn_0=(Button)?findViewById(R.id.btn_0);
		btn_1=(Button)?findViewById(R.id.btn_1);
		btn_2=(Button)?findViewById(R.id.btn_2);
		btn_3=(Button)?findViewById(R.id.btn_3);
		btn_4=(Button)?findViewById(R.id.btn_4);
		btn_5=(Button)?findViewById(R.id.btn_5);
		btn_6=(Button)?findViewById(R.id.btn_6);
		btn_7=(Button)?findViewById(R.id.btn_7);
		btn_8=(Button)?findViewById(R.id.btn_8);
		btn_9=(Button)?findViewById(R.id.btn_9);
		btn_point=(Button)?findViewById(R.id.btn_point);
		btn_clear=(Button)?findViewById(R.id.btn_clear);
		btn_del=(Button)?findViewById(R.id.btn_del);
		btn_plus=(Button)?findViewById(R.id.btn_plus);
		btn_minus=(Button)?findViewById(R.id.btn_minus);
		btn_multiply=(Button)?findViewById(R.id.btn_multiply);
		btn_divide=(Button)?findViewById(R.id.btn_divide);
		btn_equal=(Button)?findViewById(R.id.btn_equal);
		//以上是實例化按鈕
		et_input=(EditText)findViewById(R.id.et_input);//實例化顯示屏
				
		btn_0.setOnClickListener((OnClickListener)?this);
		btn_1.setOnClickListener((OnClickListener)?this);
		btn_2.setOnClickListener((OnClickListener)?this);
		btn_3.setOnClickListener((OnClickListener)?this);
		btn_4.setOnClickListener((OnClickListener)?this);
		btn_5.setOnClickListener((OnClickListener)?this);
		btn_6.setOnClickListener((OnClickListener)?this);
		btn_7.setOnClickListener((OnClickListener)?this);
		btn_8.setOnClickListener((OnClickListener)?this);
		btn_9.setOnClickListener((OnClickListener)?this);
		btn_point.setOnClickListener((OnClickListener)?this);
		btn_plus.setOnClickListener((OnClickListener)?this);
		btn_clear.setOnClickListener((OnClickListener)?this);
		btn_del.setOnClickListener((OnClickListener)?this);
		btn_plus.setOnClickListener((OnClickListener)?this);
		btn_minus.setOnClickListener((OnClickListener)?this);
		btn_multiply.setOnClickListener((OnClickListener)?this);
		btn_divide.setOnClickListener((OnClickListener)?this);
		btn_equal.setOnClickListener((OnClickListener)?this);
		//設置按鈕的點擊事件
	}

	@Override
	public?boolean?onCreateOptionsMenu(Menu?menu)?{
		//?Inflate?the?menu;?this?adds?items?to?the?action?bar?if?it?is?present.
		getMenuInflater().inflate(R.menu.main,?menu);
		return?true;
	}

	@Override
	public?boolean?onOptionsItemSelected(MenuItem?item)?{
		//?Handle?action?bar?item?clicks?here.?The?action?bar?will
		//?automatically?handle?clicks?on?the?Home/Up?button,?so?long
		//?as?you?specify?a?parent?activity?in?AndroidManifest.xml.
		int?id?=?item.getItemId();
		if?(id?==?R.id.action_settings)?{
			return?true;
		}
		return?super.onOptionsItemSelected(item);
	}

	@SuppressWarnings("null")
	@Override
	public?void?onCancel()?{
		Object?v?=?null;
		//?TODO?Auto-generated?method?stub
		String?str=et_input.getText().toString();//獲取顯示屏當前內容
		switch?(((View)?v).getId())?{
		case?R.id.btn_0:
		case?R.id.btn_1:
		case?R.id.btn_2:
		case?R.id.btn_3:
		case?R.id.btn_4:
		case?R.id.btn_5:
		case?R.id.btn_6:
		case?R.id.btn_7:
		case?R.id.btn_8:
		case?R.id.btn_9:
		case?R.id.btn_point:
			if(clear_flag){
				clear_flag=false;
				str="?";
				et_input.setText("?");
			}
			et_input.setText(str+((Button)v).getText());//點擊按鈕就在輸入框顯示
			break;
		case?R.id.btn_plus:
		case?R.id.btn_minus:
		case?R.id.btn_multiply:
		case?R.id.btn_divide:
			if(clear_flag){
				clear_flag=false;
				str="?";
				et_input.setText("?");
			}
			et_input.setText(str+"?"+((Button)v).getText()+"?");
			break;
		case?R.id.btn_del:
			if(clear_flag){
				clear_flag=false;
				str="?";
				et_input.setText("?");
			}else?if(str!=null&&!str.equals("?")){//如果不為空
			et_input.setText(str.substring(0,str.length()-1));
			}
			break;
		
		case?R.id.btn_clear:
			clear_flag=false;
			str="?";
			et_input.setText("?");
			break;
		case?R.id.btn_equal:
			getResult();
			break;
		}
	}
	
	//獲得運算結果
	private?void?getResult(){
		String?exp=et_input.getText().toString();//獲取顯示屏當前內容
		if(exp==null||exp.equals("?")){		//是空的沒有結果
			return;
		}
		if(!exp.contains("?")){			//如果沒有空格,就是只有數字直接返回
			return;
		}
		if(clear_flag){
			clear_flag=false;//點等號后標示為假
			return;
		}
		clear_flag=true;
		double?result=0;
		String?s1=exp.substring(0,exp.indexOf("?"));//運算符前面的字符串
		String?op=exp.substring(exp.indexOf("?")+1,exp.indexOf("?")+2);//運算符
		String?s2=exp.substring(exp.indexOf("?")+3);//運算符后面的字符串
		if(!s1.equals("?")&&!s2.equals("?")){			//s1和s2都不為空,進行強制類型轉換
			double?d1=Double.parseDouble(s1);
			double?d2=Double.parseDouble(s2);
			if(op.equals("+")){
				result=d1+d2;
			}else?if(op.equals("-")){
				result=d1-d2;
			}else?if(op.equals("*")){
				result=d1*d2;
			}else?if(op.equals("/")){
				if(d2==0){
					result=0;
				}else{
					result=d1/d2;
				}
			}
			if(!s1.contains(".")&&!s2.contains(".")&&op.equals("/")){//如果兩個字符串都沒有點,就強制轉換
				int?r=(int)result;
				et_input.setText(r+"?");
			}else{
				et_input.setText(result+"?");
			}
		}else?if(!s1.equals("?")&&s2.equals("?")){
			et_input.setText(exp);
		}else?if(s1.equals("?")&&!s2.equals("?")){
			double?d2=Double.parseDouble(s2);
			if(op.equals("+")){
				result=0+d2;
			}else?if(op.equals("-")){
				result=0-d2;
			}else?if(op.equals("*")){
				result=0;
			}else?if(op.equals("/")){
					result=0;
				}
			if(!s2.contains(".")){
				int?r=(int)result;
				et_input.setText(r+"?");
			}else{
				et_input.setText(result+"?");
			}
		}else{
			et_input.setText("?");
		}
		
	}
}


正在回答

2 回答

你的空格有的不對,你可以再看看視頻,空格有的是""-------有的是" "注意

0 回復 有任何疑惑可以回復我~

你的代碼我并沒有仔細看,但是如果是可以安裝但是點擊就閃退,應該是點擊事件有問題,你看一下日志或者debug一下。

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
Android攻城獅的第一門課(入門篇)
  • 參與學習       312492    人
  • 解答問題       4931    個

想快速掌握Android應用開發基礎,選擇學習這門課程就對了。

進入課程

用手機調試,根本打不開,點一下就閃退怎么回事啊?

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號