【Android】第5章(2) 按鈕和文本框

来源:http://www.cnblogs.com/rainmj/archive/2016/02/07/5184551.html
-Advertisement-
Play Games

分類:C#、Android、VS2015; 創建日期:2016-02-07 一、簡介 1、Button 常規按鈕。 2、TextView 文本視圖,其功能和WPF的TextBlock控制項類似,【工具箱】中提供的3個組件實際上是同一個TextView控制項用不同的屬性來區分的,這3個不同的屬性在【工具箱


分類:C#、Android、VS2015;

創建日期:2016-02-07

一、簡介

1、Button

常規按鈕。

2、TextView

文本視圖,其功能和WPF的TextBlock控制項類似,【工具箱】中提供的3個組件實際上是同一個TextView控制項用不同的屬性來區分的,這3個不同的屬性在【工具箱】中對應的名稱如下:

  • Text(Large):大字體的TextView
  • Text(Medium):中等字體的TextView
  • Text(small):小字體的TextView

3、EditText

文本框,其功能和WinForm的TextBox類似,區別僅是WinForm的TextBox在【工具箱】中只有一個,然後通過屬性設置是普通文本還是密碼輸入;而Android的EditText實際上也是通過屬性來區分是普通文本還是密碼輸入,但在工具箱中分別以組件的形式提供了,這2個不同的屬性在【工具箱】中對應的名稱如下:

  • PlainText:常規的EditText
  • Password:密碼輸入的EditText

二、示例1—Demo1EditText

本示例演示如何功能:

  • 在文本框中輸入信息時立即在另一個文本框中顯示所鍵入的字元。
  • Toast基本用法。
  • 常規文本框和密碼輸入文本框的基本用法。

1、運行截圖

image

2、主要設計步驟

(1)添加demo01_EditTextaxml文件
<?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="文本框基本用法"
      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)添加Demo01EditText.cs文件

先在項目根目錄下添加一個SrcActivity文件夾,然後在該文件夾下添加.cs文件,這些文件選擇的模板都是【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.txtResult);
            txtResult.SetTextColor(Color.Red);
            txtResult.Enabled = false;

            var txt1 = FindViewById<EditText>(Resource.Id.editText1);
            txt1.TextChanged += (s, e) =>
            {
                txtResult.Text = "輸入的內容為:" + txt1.Text;
            };

            var txt2 = FindViewById<EditText>(Resource.Id.editText2);
            txt2.TextChanged += (s, e) =>
            {
                txtResult.Text = "輸入的內容為:" + txt2.Text;
            };
        }
    }
}

運行即得到截圖所示的結果。

如果希望在文本輸入過程中立即判斷鍵入的是哪個字元,可利用下麵的事件來實現(用模擬器測試時,僅在硬體鍵盤開啟時才有效):

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;
    }
};

 

 

三、示例2--Demo02Login

在一個應用中,登錄是最最基本的界面,該例子演示如何利用Button、TextView、EditText基本控制項開發一個簡單的登錄視窗。

運行截圖:

image

主要設計步驟:

(1)在layout文件夾下添加demo02_Login.axml文件。在該文件的【設計】視圖中,從【工具箱】中拖放以下控制項:

Text(Medium):生成中等的TextView

PlainText:生成明文輸入的EditText

Password:生成密碼輸入的EditText

Button:生成Button

(2)在【屬性】視窗中設置各控制項對應的屬性。最後生成的代碼如下:

<?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="用戶名"
      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="密碼"
      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="登錄"
      android:layout_width="100dp"
      android:layout_height="wrap_content"
      android:id="@+id/buttonLogin"
      android:layout_gravity="center_horizontal" />
</LinearLayout>

(3)保存所有打開的文件,以便能在.cs中鍵入代碼時能看到智能提示。說明:如果在.cs文件中仍然看不到ID的智能提示,單擊【解決方案資源管理器】上方的【刷新】按鈕即可。

(4)在SrcActivity文件夾下添加Demo02Login.cs文件,將代碼改為下麵的內容:

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;  //技巧:按+=後,連續按兩次<Tab>鍵
        }

        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("用戶名:{0}, 密碼:{1}", userName.Text, pwd.Text),
                //技巧:按空格
                ToastLength.Long).Show();
        }
    }
}

運行,即得到截圖所示的效果。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 用VS新建WinForm程式,窗體上是三個文本框和一個按鈕。可以自己構造正則表達式,自己修改匹配內容正則表達是要提取的部分為hewenqitext代碼如下: 1 using System; 2 using System.Text.RegularExpressions; 3 using System.
  • 分類:C#、Android、VS2015; 創建日期:2016-02-09 一、在AssemblyInfo.cs文件中配置應用程式清單 前面的章節我們說過,除了在AndroidManifest.xml文件中配置應用程式清單外,還可以在AssemblyInfo.cs文件中配置應用程式清單。 在Asse
  • [ASP.NET MVC] ASP.NET Identity登入技術剖析 前言 ASP.NET Identity是微軟所貢獻的開源項目,用來提供ASP.NET的驗證、授權等等機制。本篇文章介紹ASP.NET Identity在執行登入功能時,與瀏覽器、還有第三方驗證服務之間的運作流程。主要為自己留個
  • 分類:C#、Android、VS2015; 創建日期:2016-02-08 在Android應用中,常用的對話框有:Toast、AlertDialog、ProgressDialog、時間選擇對話框、日期選擇對話框等。這一章主要介紹這些常用對話框的基本用法。 本章源程式共有4個示例,這些示例都在同一個
  • 前一段時間們需要對一個List<Model>集合去重,情況是該集合中會出現多個Name屬性值相同的,但是其他屬性值不同的數據。 在這種情況下,需求要只保留其中一個就好。 我覺得遍歷和HashSet都不是我想要的,便採用了一下方式 定義Compare類,繼承IEqualityComparer介面 pu
  • 看了《CLR via C#》的17章委托後,為自己做一點淺顯的總結,也分享給需要的人。 .NET通過委托來提供一種回調函數機制,.NET委托提供了很多功能,例如確保回調方法是類型安全的(CLR重要目標)。委托好允許順序調用多個方法(委托鏈),並且支持調用靜態方法和實例方法。 委托的基本語法就不多說了
  • 分類:C#、Android、VS2015; 創建日期:2016-02-07 一、簡介 圖庫(也叫畫廊)是一個佈局小部件,用於在可水平滾動的列表中顯示每一副圖片,當前所選的圖片將置於視圖的中心。 註意:Android已經棄用了這個小部件,棄用的原因是用Galery實現的效率比較低,官方的建議是改為用H
  • 分類:C#、Android、VS2015; 創建日期:2016-02-07 一、簡介 1、CheckBox 覆選 【Checked】屬性:是否選中。 2、RadioButton 單選 【Checked】屬性:是否選中。 【RadioGroup】屬性:RadioButton的分組容器。註意必須將Rad
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...