編輯:關於Android編程
package com.example.zhouhui.mysimplecalculator; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { EditText ContentText; Button CButton; Button DivButton; Button MulButton; Button DelButton; Button SevenButton; Button EightButton; Button NineButton; Button SubButton; Button FourButton; Button FiveButton; Button SixButton; Button AdditionButton; Button OneButton; Button TwoButton; Button ThreeButton; Button RemainderButton; Button ZeroButton; Button EqualButton; boolean isClickEqu = false; // 是否點擊= @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ContentText = (EditText) findViewById(R.id.content_text); CButton = (Button) findViewById(R.id.c_button); DivButton = (Button) findViewById(R.id.div_button); MulButton = (Button) findViewById(R.id.mul_button); SubButton = (Button) findViewById(R.id.sub_button); AdditionButton = (Button) findViewById(R.id.addition_button); RemainderButton = (Button) findViewById(R.id.remainder_button); DelButton = (Button) findViewById(R.id.del_button); SevenButton = (Button) findViewById(R.id.seven_button); EightButton = (Button) findViewById(R.id.eight_button); NineButton = (Button) findViewById(R.id.nine_button); FourButton = (Button) findViewById(R.id.four_button); FiveButton = (Button) findViewById(R.id.five_button); SixButton = (Button) findViewById(R.id.six_button); OneButton = (Button) findViewById(R.id.one_button); TwoButton = (Button) findViewById(R.id.two_button); ThreeButton = (Button) findViewById(R.id.three_button); ZeroButton = (Button) findViewById(R.id.zero_button); EqualButton = (Button) findViewById(R.id.equal_button); CButton.setOnClickListener(new MyClickListener()); DivButton.setOnClickListener(new MyClickListener()); MulButton.setOnClickListener(new MyClickListener()); DelButton.setOnClickListener(new MyClickListener()); SevenButton.setOnClickListener(new MyClickListener()); EightButton.setOnClickListener(new MyClickListener()); NineButton.setOnClickListener(new MyClickListener()); SubButton.setOnClickListener(new MyClickListener()); FourButton.setOnClickListener(new MyClickListener()); FiveButton.setOnClickListener(new MyClickListener()); SixButton.setOnClickListener(new MyClickListener()); AdditionButton.setOnClickListener(new MyClickListener()); OneButton.setOnClickListener(new MyClickListener()); TwoButton.setOnClickListener(new MyClickListener()); ThreeButton.setOnClickListener(new MyClickListener()); RemainderButton.setOnClickListener(new MyClickListener()); ZeroButton.setOnClickListener(new MyClickListener()); EqualButton.setOnClickListener(new MyClickListener()); } @Override protected void onStop() { super.onStop(); } @Override protected void onDestroy() { super.onDestroy(); } @Override protected void onPause() { super.onPause(); } @Override protected void onResume() { super.onResume(); } @Override protected void onStart() { super.onStart(); } public double compute(String text) { double result = 0; double num1 = 0; double num2 = 0; char temp = 0; if(text.contains("+")) { temp = '+'; } else if(text.contains("-")) { temp = '-'; } else if(text.contains("*")) { temp = '*'; } else if(text.contains("/")) { temp = '/'; } else if(text.contains("%")) { temp = '%'; } try{ num1 = Double.valueOf(text.substring(0,text.lastIndexOf(temp))); num2 = Double.valueOf(text.substring(text.lastIndexOf(temp) + 1)); }catch (Exception e) { num1 = 0; num2 = 0; } if(temp == '+') { result = num1 + num2; } else if(temp == '-') { result = num1 - num2; } else if(temp == '*') { result = num1 * num2; } else if(temp == '/') { result = num1 / num2; } else if(temp == '%') { result = num1 % num2; } Log.i("MainActivity",result + " = " + num1 + temp + num2); return result; } public void clickNumber(int n) { if(isClickEqu) { ContentText.setText(String.valueOf(n)); isClickEqu = false; } else { String text = ContentText.getText().toString(); ContentText.setText(text + String.valueOf(n)); } } public void clickOp(char temp) { String text = ContentText.getText().toString(); if(temp == '+' || temp == '-' || temp == '*' || temp == '/' || temp == '%') { if(text == null || text.equals("") || text.contains("+") || text.contains("-") || text.contains("*") || text.contains("/") || text.contains("%")) { return; } isClickEqu = false; ContentText.setText(text + temp); } else if(temp == '=') { if(text == null || text.equals("")) { return; } double result; result = compute(text); ContentText.setText(String.valueOf(String.valueOf(result))); isClickEqu=true; } else if(temp == 'd') { isClickEqu=false; try { ContentText.setText(text.substring(0, text.length()-1)); } catch (Exception e) { ContentText.setText(""); } } else if(temp == 'c') { isClickEqu=false; ContentText.setText(""); } } class MyClickListener implements View.OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.c_button: clickOp('c'); break; case R.id.del_button: clickOp('d'); break; case R.id.addition_button: clickOp('+'); break; case R.id.sub_button: clickOp('-'); break; case R.id.mul_button: clickOp('*'); break; case R.id.div_button: clickOp('/'); break; case R.id.remainder_button: clickOp('%'); break; case R.id.equal_button: clickOp('='); break; case R.id.zero_button: clickNumber(0); break; case R.id.one_button: clickNumber(1); break; case R.id.two_button: clickNumber(2); break; case R.id.three_button: clickNumber(3); break; case R.id.four_button: clickNumber(4); break; case R.id.five_button: clickNumber(5); break; case R.id.six_button: clickNumber(6); break; case R.id.seven_button: clickNumber(7); break; case R.id.eight_button: clickNumber(8); break; case R.id.nine_button: clickNumber(9); break; default: break; } } } }以及單元測試:
package com.example.zhouhui.mysimplecalculator; import android.app.Instrumentation; import android.test.ActivityInstrumentationTestCase2; import android.widget.Button; import android.widget.EditText; import org.junit.Test; /** * Created by xxxxxxx on 16/7/7. */ public class MainActivityTest extends ActivityInstrumentationTestCase2{ private MainActivity mActivity; Instrumentation mInstrumentation; EditText ContentText; Button CButton; Button DivButton; Button MulButton; Button DelButton; Button SevenButton; Button EightButton; Button NineButton; Button SubButton; Button FourButton; Button FiveButton; Button SixButton; Button AdditionButton; Button OneButton; Button TwoButton; Button ThreeButton; Button RemainderButton; Button ZeroButton; Button EqualButton; public MainActivityTest() { super(MainActivity.class); } @Override public void setUp() throws Exception { super.setUp(); setActivityInitialTouchMode(false); mInstrumentation = getInstrumentation(); //獲取被測的Activity mActivity = getActivity(); //獲取這個活動界面的控件 ContentText = (EditText) mActivity .findViewById(com.example.zhouhui.mysimplecalculator.R.id.content_text); CButton = (Button) mActivity.findViewById(R.id.c_button); DivButton = (Button) mActivity.findViewById(R.id.div_button); MulButton = (Button) mActivity.findViewById(R.id.mul_button); SubButton = (Button) mActivity.findViewById(R.id.sub_button); AdditionButton = (Button) mActivity.findViewById(R.id.addition_button); RemainderButton = (Button) mActivity.findViewById(R.id.remainder_button); DelButton = (Button) mActivity.findViewById(R.id.del_button); SevenButton = (Button) mActivity.findViewById(R.id.seven_button); EightButton = (Button) mActivity.findViewById(R.id.eight_button); NineButton = (Button) mActivity.findViewById(R.id.nine_button); FourButton = (Button) mActivity.findViewById(R.id.four_button); FiveButton = (Button) mActivity.findViewById(R.id.five_button); SixButton = (Button) mActivity.findViewById(R.id.six_button); OneButton = (Button) mActivity.findViewById(R.id.one_button); TwoButton = (Button) mActivity.findViewById(R.id.two_button); ThreeButton = (Button) mActivity.findViewById(R.id.three_button); ZeroButton = (Button) mActivity.findViewById(R.id.zero_button); EqualButton = (Button) mActivity.findViewById(R.id.equal_button); } @Test public void testonCreate() throws Exception { } @Test public void testPreCondition() { assertNotNull(mActivity); assertNotNull(ContentText); assertNotNull(CButton); assertNotNull(DivButton); assertNotNull(MulButton); assertNotNull(SubButton); assertNotNull(AdditionButton); assertNotNull(RemainderButton); assertNotNull(DelButton); assertNotNull(SevenButton); assertNotNull(EightButton); assertNotNull(NineButton); assertNotNull(FourButton); assertNotNull(FiveButton); assertNotNull(SixButton); assertNotNull(OneButton); assertNotNull(TwoButton); assertNotNull(ThreeButton); assertNotNull(ZeroButton); assertNotNull(EqualButton); } @Test public void testcompute() throws Exception { assertEquals(0d, mActivity.compute("0")); assertEquals(0d, mActivity.compute("1234")); assertEquals(3d, mActivity.compute("1+2")); assertEquals(55d,mActivity.compute("11+44")); assertEquals(11111111155d,mActivity.compute("11111111111+44")); assertEquals(1d, mActivity.compute("2-1")); assertEquals(-1d, mActivity.compute("1-2")); assertEquals(2d, mActivity.compute("1*2")); assertEquals(2d, mActivity.compute("2/1")); assertEquals(1.5d, mActivity.compute("3/2")); assertEquals(Double.POSITIVE_INFINITY, mActivity.compute("2/0")); assertEquals(Double.NaN, mActivity.compute("5%0")); assertEquals(1d, mActivity.compute("3%2")); assertEquals(0d, mActivity.compute("3%1")); } @Test public void testclickNumber() throws Exception { } @Test public void testclickOp() throws Exception { } }
適配:即當前應用在相同的手機上面顯示相同的效果。適配前需要首先確定當前手機所屬像素密度類型(如:xhdpi、hdpi、mdpi等) 像素密度:每英寸上分布的像素點個數,單
0x00原理部分我不獻丑了,上面3篇文章說的很清楚,我直接實戰,講述從0開始如何最終實現加固的整個過程,踩了不少坑。0x01第一步創建被加固Apk,就是你的源碼Apk。
我們在常用的電商或者旅游APP中,例如美團,手機淘寶等等,都能夠看的到有那種下拉式的二級列表菜單。具體如圖所示:上面兩張圖就是美團的一個二級列表菜單的一個展示。我相信很多
1、事務2、命令行操作數據庫