Android Reconstruction Program

Source: Internet
Author: User

InAndroidObject, We Need To refactor an instance. Reconstruction is to adjust the program code to improve the quality and performance of the software without changing the existing functions of the software, so that the program design mode and architecture become more reasonable, improve software scalability and maintainability.

1. What are the benefits of refactoring a program: when a program is written, what we need to do is to modify it and maintain it. Once the program grows and becomes more complex, it is difficult to maintain the state of chaos, so you have to delete and rewrite the program. So if we can re-organize or partially rewrite the code in some ways to make the code easy to maintain again, we can save a lot of time for ourselves, * meet more challenges.

2. Definition of refactoring: the process of modifying the program code to increase readability or simplify the program structure without affecting the output result. A proprietary term is refactoring.

3. demo

Java code:

 
 
  1. package eoe.demo;   
  2. import java.text.DecimalFormat;   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. import android.view.View;   
  6. import android.widget.Button;   
  7. import android.widget.EditText;   
  8. import android.widget.TextView;   
  9.  
  10. public classBMIextends Activity{   
  11. @Override   
  12. public void onCreate(Bundle savedInstanceState){   
  13. super.onCreate(savedInstanceState);   
  14. setContentView(R.layout.main);   
  15. findView();   
  16. setListensers();   
  17. }   
  18. private Button btn_calc;   
  19. private EditText field_height;   
  20. private EditText field_weight;   
  21. private TextView view_result;   
  22. private TextView view_suggest;   
  23. private void findView(){   
  24. btn_calc = (Button)findViewById(R.id.submit);   
  25. field_height =(EditText)findViewById(R.id.height);   
  26. field_weight = (EditText)findViewById(R.id.weight);   
  27. view_result = (TextView)findViewById(R.id.result);   
  28. view_suggest = (TextView)findViewById(R.id.suggest);   
  29. private void setListensers(){   
  30. btn_calc.setOnClickListener(clacBMI);   
  31. }  
  32. private Button.OnClickListener clacBMI = new Button.OnClickListener(){   
  33. public void onClick(View v){   
  34. DecimalFormat nf = new DecimalFormat("0.0");   
  35. double height = Double.parseDouble(field_height.getText().toString())/100;   
  36. double weight = Double.parseDouble(field_weight.getText().toString());   
  37. doubleBMI = weight / (height*height);   
  38. view_result.setText(getText(R.string.bmi_result)+nf.format(BMI));   
  39. if(BMI > 25){   
  40. view_suggest.setText(R.string.advice_heavy);   
  41. }else if(BMI < 20){   
  42. view_suggest.setText(R.string.advice_light);   
  43. }else{   
  44. view_suggest.setText(R.string.advice_average);   
  45. }   
  46. }  
  47. };   
  48. }  

4. in the above Code: Make sure to unify the naming method of the Identity parameter: add the "btn _" prefix before the button's identity parameter, you can add the "field _" prefix before the field identification parameter and the "view _" prefix before the displayed parameter. Benefit: understand the meaning of variables and make the program better read.

Android message mechanism

Programmers should know that refactoring

Build an Android Development Environment

Object-oriented reconstruction of C Language

Android Network Connection processing learning notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.