cdxy.me
Footprints on cyber security and Python

#1.0 版本

 

功能:

选择付款者,选择受益者,添加单次消费记录

计算负债情况

本地存取数据

某些功能弹窗提示

对不合法输入及操作的处理

 

部分组件实现代码

toast

Toast toast = Toast.makeText(MainActivity.this, "tips!", 1000);
toast.show();

checkbox

final CheckBox check1= (CheckBox)findViewById(R.id.checkBox1);
	    final CheckBox check2= (CheckBox)findViewById(R.id.checkBox2);
	    final CheckBox check3= (CheckBox)findViewById(R.id.checkBox3);
	    final CheckBox check4= (CheckBox)findViewById(R.id.checkBox4);
	    final CheckBox check5= (CheckBox)findViewById(R.id.checkBox5);
	    
	    OnCheckedChangeListener myCheckedListener1=new OnCheckedChangeListener(){
	        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
	           if(isChecked){
	               person[0] = true;
	           }else{
	        	   person[0] = false;
	           }
	        }
	    };
	    OnCheckedChangeListener myCheckedListener2=new OnCheckedChangeListener(){
	        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
	           if(isChecked){
	               person[1] = true;
	           }else{
	        	   person[1] = false;
	           }
	        }
	    };
	    OnCheckedChangeListener myCheckedListener3=new OnCheckedChangeListener(){
	        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
	           if(isChecked){
	               person[2] = true;
	           }else{
	        	   person[2] = false;
	           }
	        }
	    };
	    OnCheckedChangeListener myCheckedListener4=new OnCheckedChangeListener(){
	        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
	           if(isChecked){
	               person[3] = true;
	           }else{
	        	   person[3] = false;
	           }
	        }
	    };
	    OnCheckedChangeListener myCheckedListener5=new OnCheckedChangeListener(){
	        public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
	           if(isChecked){
	               person[4] = true;
	           }else{
	        	   person[4] = false;
	           }
	        }
	    };
	    
	    check1.setOnCheckedChangeListener(myCheckedListener1);
	    check2.setOnCheckedChangeListener(myCheckedListener2);
	    check3.setOnCheckedChangeListener(myCheckedListener3);
	    check4.setOnCheckedChangeListener(myCheckedListener4);
	    check5.setOnCheckedChangeListener(myCheckedListener5);

 

Button(onClick onLongClick)

Button button2=(Button)findViewById(R.id.button2);
		button2.setOnLongClickListener(new OnLongClickListener() {
			public boolean onLongClick(View v) {
				is_long = true;
				Toast toast = Toast.makeText(MainActivity.this, "内存已清除", 1000);
				toast.show();
				Toast toast1 = Toast.makeText(MainActivity.this, "于是我们又度过了一段愉快的时光", 1000);
				toast1.show();
				//清除数据及存储
				cash[0] = cash[1] =cash[2]=cash[3]=cash[4] = 0;
				WriteFiles("0 0 0 0 0");
				//再次输出
				TextView text1=(TextView) findViewById(R.id.textView2);
				//String y1 = "付款者是" + paid_name +"   "+String.valueOf(paid_cash);
				String setText = "徐越:   " + String.valueOf(cash[0]) + "\n" +
						"衣帅:   " + String.valueOf(cash[1])+ "\n" +
						"王拓:   " + String.valueOf(cash[2])+ "\n" + 
						"龚方韶华:   " + String.valueOf(cash[3])+ "\n" +
						"黄紫薇:   " + String.valueOf(cash[4]);
				text1.setText(setText);
				
				return false;
			}
		});
		button2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(!is_long){
				Toast toast = Toast.makeText(MainActivity.this, "亲!真的要这样嘛?", 1000);
				toast.show();
				Toast toast1 = Toast.makeText(MainActivity.this, "请长按2秒", 1000);
				toast1.show();
				}
			}
		});

 

spinner

Spinner spinner = (Spinner)findViewById(R.id.spinner1);
		ArrayAdapter<String> adapter =new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, person_name);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setPrompt("请选择付款者");
		
		spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
			public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
				paid_name = arg0.getItemAtPosition(arg2).toString();                                      
			}
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

 

 

数据存储——txt

//保存文件内容
	public void WriteFiles(String content){
			 try {
				FileOutputStream fos = openFileOutput("a.txt", MODE_WORLD_READABLE + MODE_WORLD_WRITEABLE);
				 fos.write(content.getBytes());
				 fos.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			 
	}
	
	//读取文件内容
	public String readFiles(){
			String content = null;
			 try {
				FileInputStream fis= openFileInput("a.txt");
				 ByteArrayOutputStream baos =  new ByteArrayOutputStream();
				byte [] buffer =  new byte[1024];
				int len = 0;
				while ((len=fis.read(buffer))!=-1) {
					baos.write(buffer, 0, len);
				}
				content = baos.toString();
				fis.close();
				baos.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			return content;
		}

 

//从txt中读取之前的数据
		//FileOutputStream fos = openFileOutput("a.txt", MODE_WORLD_READABLE + MODE_WORLD_WRITEABLE);
		File file1= new File(this.getFilesDir().toString() + "/a.txt");
		//Log.i("info", this.getFilesDir().toString());
		if(file1.exists()){
			String read_file = readFiles();
			String[] str = read_file.toString().split(" ");//转换为String数组
			for(int i = 0;i<5;i++){
				cash[i] = Double.parseDouble(str[i]);//存入cash数组
			}
		}