In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Android how to restructure the program, for this problem, this article details the corresponding analysis and answer, hoping to help more small partners who want to solve this problem find a simpler and easier way.
Defining objects in Android requires us to refactor an instance. Refactoring is to improve the quality and performance of software by adjusting the program code without changing the existing functions, so as to make the design mode and architecture of the program more reasonable and improve the scalability and maintainability of software.
1. What's good about refactoring programs: When a program is written, all we have to do is modify it and maintain it. Once the program grows longer and more complex, chaotic to the point where it cannot be maintained, it has to be deleted and rewritten. So if we can reorganize or partially rewrite the code in some way to make it easy to maintain again, we can save ourselves a lot of time * and meet more challenges.
2. Definition of refactoring: The process of making changes to program code to increase readability or simplify program structure without affecting the output. There is a proper term called refactoring.
3. demo
Java code:
package eoe.demo; import java.text.DecimalFormat; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public classBMIextends Activity{ @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); findView(); setListensers(); } private Button btn_calc; private EditText field_height; private EditText field_weight; private TextView view_result; private TextView view_suggest; private void findView(){ btn_calc = (Button)findViewById(R.id.submit); field_height =(EditText)findViewById(R.id.height); field_weight = (EditText)findViewById(R.id.weight); view_result = (TextView)findViewById(R.id.result); view_suggest = (TextView)findViewById(R.id.suggest); private void setListensers(){ btn_calc.setOnClickListener(clacBMI); } private Button.OnClickListener clacBMI = new Button.OnClickListener(){ public void onClick(View v){ DecimalFormat nf = new DecimalFormat("0.0"); double height = Double.parseDouble(field_height.getText().toString())/100; double weight = Double.parseDouble(field_weight.getText().toString()); doubleBMI = weight / (height*height); view_result.setText(getText(R.string.bmi_result)+nf.format(BMI)); if(BMI > 25){ view_suggest.setText(R.string.advice_heavy); }else if(BMI < 20){ view_suggest.setText(R.string.advice_light); }else{ view_suggest.setText(R.string.advice_average); } } }; }
4. In the above code: pay attention to unify the naming method of identification parameters: add "btn_" prefix before the identification parameters of buttons, add "field_" prefix before the identification parameters of input fields, and add "view_" prefix before the parameters used for display. Benefits: Understanding the meaning of variables makes the program easier to read.
About Android how to restructure the program questions to share the answer here, I hope the above content can be of some help to everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.