簡體   English   中英

在Mono Android(C#)中的編輯文本控件中獲取用戶輸入

[英]getting the user input in edit text control in mono android (C#)

我寫了一個簡單的android應用程序來測試c#中的mono android。 我的程序是有一個Edit Text控件(或當我在main.axml文件中創建UI時是控件名稱的純文本),並有一個button.user在編輯文本中鍵入一些內容並按下按鈕我想要的只是獲取用戶輸入。但是我無法通過Text屬性訪問它。

旁注:我的主要程序是通過Socket將此文本發送到服務器,但是當我想獲取字符串並將其放入緩沖區時,它始終不返回任何內容。 這是我的測試程序:

     protected override void OnCreate(Bundle bundle) {
         base.OnCreate(bundle);
         SetContentView(Resource.Layout.Main);
         EditText input = FindViewById(Resource.Id.editText1);
         Button send = FindViewById<Button>(Resource.Id.button1);
         send.Click += (sender, e) => {             
              string str = input.Text;
         };
     }

這是我實際的套接字程序:

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.Main);
        EditText iptext = FindViewById<EditText>(Resource.Id.editText1);
        EditText porttext = FindViewById<EditText>(Resource.Id.editText2);
        EditText input = FindViewById<EditText>(Resource.Id.editText3);
        Button send = FindViewById<Button>(Resource.Id.button1);
        send.Click += (sender, e) => {
            *connetpoint = new IPEndPoint(IPAddress.Parse(iptext.Text) ,Convert.ToInt32(porttext.Text));
            sc.Connect(connetpoint);
            sendbuffer = System.Text.Encoding.UTF8.GetBytes(input.Text);
            sc.Send(sendbuffer);
        };

    }

但奇怪的是,*行完美地工作,在其中我得到了其他2個編輯文本輸入(服務器的IP和端口)。

我看不到您的代碼有問題,並且無法重新創建您的問題。 以下代碼在模擬器和設備上均適用:

var button = FindViewById<Button>(Resource.Id.MyButton);
var et1 = FindViewById<EditText>(Resource.Id.editText1);
var et2 = FindViewById<EditText>(Resource.Id.editText2);
var et3 = FindViewById<EditText>(Resource.Id.editText3);

button.Click += (s,e) =>
{
    var text1 = et1.Text;
    var text2 = et2.Text;
    var text3 = et3.Text;
    Toast.MakeText(this, string.Format("T1: {0}, T2: {1}, T3: {2}", text1, text2, text3), ToastLength.Short).Show();
};

您如何驗證EditText確實包含值? 可能是由於某些原因,調試器的時間很糟糕,並且報告它為空,而實際上卻不為空。 在這種情況下,我會向Xamarin報告。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM