In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the programming method of Android interface". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the programming method of Android interface"?
There are two basic methods of Android interface programming, one is to dynamically create components in the code, and to combine these components with Layout to form a complex interface presentation. One is to write the layout Layout graphically. These layouts are saved in the XML file, compiled into resources, loaded by the Activity in the program (setContentView ()), and then manipulated by findViewById to obtain references to each interface component. For most people, they like the most intuitive way, that is, the way it is generated dynamically in the code.
First, layout management (Layout)
Each interface component is a subclass of View and can occupy a separate screen, but the really useful interface is the combination of these components. In Android, all kinds of Layout are used for layout management, which is basically the same as some AWT,SWING interfaces in traditional J2SE.
Second, a single interface element:
In the Hello World example I mentioned earlier, I talked about a piece of code like this. In Activity.
Public class HelloActivity extends Activity {/ * * Called when the activity is first created. * / @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); TextView tv = new TextView (this); tv.setText ("Hello, World!"); this.setContentView (tv);}
Layout is not used here, which is a separate component approach. You can also change it to:
Super.onCreate (savedInstanceState); Button btn = new Button (this); btn.setText ("TestButton"); this.setContentView (btn)
Compile and run, there will be a full-screen Button, of course, this is not the practical interface you want. Then we will use Layout to lay out.
Super.onCreate (savedInstanceState); Button btn = new Button (this); btn.setText ("TestButton"); Button btn2 = new Button (this); btn2.setText ("TestButton2"); LinearLayout layout = new LinearLayout (this); layout.setOrientation (LinearLayout.VERTICAL); layout.addView (btn); layout.addView (btn2); this.setContentView (layout)
Compile and run, you can see two buttons arranged up and down. Of course, anyone who has done AWT,SWING on PC is no stranger to the use of the layout manager, so I won't repeat it here.
So how to respond to the event: guess? It is not difficult to guess that in AWT and in the J2ME of mobile phones, Listener is used to deal with event responses, and Android is not vulgar. This is the same as Observer in Blackberry,Symbian. It is the observer pattern that uses the design pattern. Let's take a look at an example that can respond to events.
Import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; public class HelloActivity extends Activity implements OnClickListener {Button btn = null; Button btn2 = null; public void onClick (View v) {if (v = = btn) {this.setTitle ("You Clicked Button1") } if (v = = btn2) {this.setTitle ("You Clicked Button2");}} @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); btn = new Button (this); btn2 = new Button (this); btn.setText ("TestButton1"); btn2.setText ("TestButton2") Btn.setOnClickListener (this); btn2.setOnClickListener (this); LinearLayout layout = new LinearLayout (this); layout.setOrientation (LinearLayout.VERTICAL); layout.addView (btn); layout.addView (btn2); this.setContentView (layout);}}
The steps are:
1. Generate two Button and configure the Click event listener as HelloActivity. This class implements the OnClickListener interface.
2. Put the layout and display two Button according to the layout.
3. Press one of the Button, generate the Click event, and call the OnClick interface function of HelloActivity.
4. For the value of View parameter, determine which View (Button) it is. Rewrite the Titile content of Activity. Note that do not compare View.getId (), by default, the ID value of each component is-1, unless the ID value is artificially set, and an ID value is automatically generated for it when visually programmed.
At this point, I believe that everyone on the "Android interface programming method is what" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.