Android---2---TextView, Button, EditText

Source: Internet
Author: User

1.TextView

TextView can be said to be one of the simplest controls in Android. It is mainly used to display a text message on the interface.


<textview        android:layout_width= "wrap_content"        android:layout_height= "wrap_content"        android:text = "@string/hello_world"/>

This is a piece of code in the HelloWorld project, android:layout_width specifies the width of the control, and the same android:layout_height specifies the height of the control.

All the controls in Android have these two properties, with three selectable values: Match_parent, Fill_parent, Wrap_content. The first two properties are actually the same, indicating that the size of the current control is the same as the size of the parent layout. Wrap_content means that the current control can be sized to wrap the contents of the period. By modifying the values represented by the Text property, you can change the text information displayed in the interface.

The default upper-left corner of the text in TextView is aligned.


<textview        android:id= "@+id/world"        android:layout_width= "fill_parent"        android:layout_height= " Wrap_content "        android:layout_gravity=" center "        android:gravity=" Center|left "        android:text=" @string/ Hello_world "        android:textcolor=" #00ff00 "        android:textsize=" 20pt "/>

In this code, you add the color and size of the text you want to display, and give the control a id,id identifier.

There are gravity and layout_gravity, what is the difference between these two?

Gravity controls the current control content display area, controls the content, layout_gravity controls the position of the current control at the parent element, and controls the entire control.

2.Button

The button is derived from TextView. There are many similarities between its attribute methods and TextView. The biggest difference between them is that TextView does not allow edits, and button allows editing.

3.EditText

This is an important control that the program uses to interact with the user, allowing the user to enter and edit content in the control, and to process the content in the program.



With these three controls we can write some simple interfaces, such as the login interface.

The first is strings.xml:


<?xml version= "1.0" encoding= "Utf-8"?><resources>    <string name= "App_name" >login</string >    <string name= "Hello_world" >hello world!</string>    <string name= "Action_settings" > Settings</string><string name= "Number" > Please enter your account </string><string name= "PSW" > Please enter your password </string ></resources>

And then the activity_main.xml in layout.


<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "Android:orienta tion= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin "android:paddingright=" @dimen/activity_horizontal_margin "android:paddingtop=" @dimen/activity_ Vertical_margin "tools:context=" com.example.login.MainActivity "> <textview android:layout_width=" wrap_ Content "android:layout_height=" wrap_content "android:text=" @string/number "/> <edittext and Roid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:id= "@+id/number"/> <T Extview android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "@strin G/PSW "/> <edittext android:layout_width="Fill_parent "android:layout_height=" wrap_content "android:inputtype=" Textpassword "android:id=" @+id/p SW "/> <button android:id=" @+id/button "android:layout_width=" Wrap_content "Android:layout_h eight= "Wrap_content" android:text= "Landing"/></linearlayout>


And finally, the Mainactivity.java under SRC.


Package Com.example.login;import Android.app.activity;import Android.os.bundle;import android.view.Menu;import Android.view.menuitem;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.toast;public class MainActivity extends Activity {//Declaration control Private EditText numbertext;private EditText pswtext;private button button; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); /Find Control Numbertext = (EditText) Findviewbyid (r.id.number);p Swtext = (EditText) Findviewbyid (R.ID.PSW); button = (button) Findviewbyid (R.id.button);//Set a listener for the button that will trigger the OnClick event under the listener when the button is touched Button.setonclicklistener (new Btonclicklistener ());} Public final class Btonclicklistener implements onclicklistener{@Overridepublic void OnClick (View v) {//TODO Auto-genera Ted method Stubstring Number = Numbertext.gettext (). toString (); String PSW = pswtext.gEttext (). toString (); String info = number + "+++++" + psw;//uses toasts to prompt for information Toast.maketext (Getapplicationcontext (), info, Toast.length_long). Show ();}} The following temporarily does not use the @overridepublic boolean oncreateoptionsmenu (Menu menu) {///inflate the menu; This adds items to the action Bar If it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);}}


Operation Result:


Android---2---TextView, Button, EditText

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.