Chapter 2 (2) buttons and text boxes, Chapter 2 button text box

Source: Internet
Author: User

Chapter 2 (2) buttons and text boxes, Chapter 2 button text box

Category: C #, Android, VS2015;

Created on: 1. Introduction

1. Button

Regular button.

2. TextView

The function of a text view is similar to that of the TextBlock control of WPF. The three components provided in the toolbox are actually differentiated by different attributes of the same TextView control, the names of these three attributes are as follows in the toolbox:

  • Text (Large): TextView with Large fonts
  • Text (Medium): TextView with Medium Fonts
  • Text (small): TextView in small font

3. EditText

Text Box. Its function is similar to that of the TextBox of WinForm. The difference is that the TextBox of WinForm only has one in the toolbox, and then the attribute is set to plain text or password input; android EditText uses attributes to distinguish between common text and Password Input. However, it is provided in the toolbox in the form of components, the names of these two different attributes are as follows in the toolbox:

  • PlainText: General EditText
  • Password: The EditText entered by the Password
Ii. Example 1-demo1edittext

This example shows how to use the following functions:

  • When entering information in the text box, the entered characters are immediately displayed in another text box.
  • Toast basic usage.
  • Common text box and Password Input text box basic usage.

1. Run

2. Major design steps (1) Add the demo01_EditTextaxml File

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "match_parent" android: layout_height = "match_parent"> <TextView android: text = "basic usage of text box" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: id = "@ + id/textView1"/> <EditText android: layout_width = "match_parent" android: layout_height = "wrap_content" android: id = "@ + id/editText1"/> <EditText android: inputType = "textPassword" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: id = "@ + id/editText2"/> <TextView android: text = "" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: id = "@ + id/txtResult" android: gravity = "center_horizontal" android: layout_marginTop = "20dp"/> </LinearLayout>
(2) Add the Demo01EditText. cs File

First, add a SrcActivity folder under the project root directory, and then add the. cs file under the folder. All the templates selected for these files are Activity ].

Using Android. app; using Android. OS; using Android. widget; using Android. graphics; namespace ch05demos. srcActivity {[Activity (Label = "TextBoxDemo")] public class Demo01EditText: Activity {protected override void OnCreate (Bundle savedInstanceState) {base. onCreate (savedInstanceState); SetContentView (Resource. layout. demo01_EditText); var txtResult = FindViewById <TextView> (Resource.Id.txt Result); txtResult. setTextColor (Color. red); txtResult. enabled = false; var txt1 = FindViewById <EditText> (Resource. id. editText1); txt1.TextChanged + = (s, e) => {txtResult. text = "the input content is:" + txt1.Text;}; var txt2 = FindViewById <EditText> (Resource. id. editText2); txt2.TextChanged + = (s, e) => {txtResult. text = "the input content is:" + txt2.Text ;};}}}

Run the command to obtain the result.

If you want to determine which character you are typing in the text input process immediately, you can use the following event (it is valid only when the hardware keyboard is enabled during simulator testing ):

EditText edittext = FindViewById<EditText>(Resource.Id.edittext);edittext.KeyPress += (s, e) =>{    e.Handled = false;    if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter) {        Toast.MakeText (this, edittext.Text, ToastLength.Short).Show ();        e.Handled = true;    }};

3. Example 2 -- Demo02Login

In an application, logon is the most basic interface. This example shows how to use the Button, TextView, and EditText controls to develop a simple logon window. Run:

Main design steps:

(1) Add the demo02_Login.axml file in the layout folder. In the design view of the file, drag and drop the following controls from the toolbox:

Text (Medium): Generate Medium TextView

PlainText: Generate EditText for PlainText Input

Password: generate the EditText entered by the Password

Button: generate a Button

(2) set the properties of each control in the Properties window. The generated code is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "match_parent" android: layout_height = "match_parent"> <TextView android: text = "username" android: textAppearance = "? Android: attr/textAppearanceMedium "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: id =" @ + id/textView1 "/> <EditText android: layout_width = "match_parent" android: layout_height = "wrap_content" android: id = "@ + id/editTextUserName"/> <TextView android: text = "password" android: textAppearance = "? Android: attr/textAppearanceMedium "android: layout_width =" match_parent "android: layout_height =" wrap_content "android: id =" @ + id/textView2 "/> <EditText android: inputType = "textPassword" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: id = "@ + id/editTextPwd"/> <Button android: text = "login" android: layout_width = "100dp" android: layout_height = "wrap_content" android: id = "@ + id/buttonLogin" android: layout_gravity = "center_horizontal"/> </LinearLayout>

(3) Save all open files so that you can see smart prompts when typing code in. cs. Note: If you still cannot see the ID prompt in the. cs file, click the refresh button above Solution Explorer.

(4) Add the Demo02Login. cs file in the SrcActivity folder and change the code to the following content:

Using System; using Android. app; using Android. OS; using Android. widget; namespace ch05demos. srcActivity {[Activity (Label = "LoginDemo")] public class Demo02Login: Activity {protected override void OnCreate (Bundle savedInstanceState) {base. onCreate (savedInstanceState); SetContentView (Resource. layout. demo02_Login); Button btn = FindViewById <Button> (Resource. id. buttonLogin); btn. click + = Btn_Click; // tip: Press + = twice in a row and press <Tab> key} private void Btn_Click (object sender, EventArgs e) {var userName = FindViewById <EditText> (Resource. id. editTextUserName); var pwd = FindViewById <EditText> (Resource. id. editTextPwd); Toast. makeText (this, string. format ("userName: {0}, password: {1}", userName. text, pwd. text), // tip: press the space ToastLength. long ). show ();}}}

Run.

Related Article

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.