Почему text2.Text = "message" не работает в моем коде? - PullRequest
0 голосов
/ 05 октября 2011

Почему text2.Text = "message" не работает в моем коде? Я хочу работать таким образом в функции см. В коде. Я разработал в Visual Stduio с моно для Android в C #

Исходный код:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace ChatClient_Android
{
[Activity(Label = "ChatClient_Android", MainLauncher = true, Icon = "@drawable/icon")]
public class MainChat : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        EditText text2 = FindViewById<EditText>(Resource.Id.text2);
     }

   private void  recieved()
   {
    text2.Text = "mesage";   // The text2 does not existe in this context 

    }
 }

}

Ответы [ 2 ]

4 голосов
/ 05 октября 2011

EditText text2 объявляется не глобальным, а для метода. Поместите EditText text2; в класс.

Должно быть так:

public class MainChat : Activity
{
    EditText text2; // <----- HERE
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.Main);
        text2 = FindViewById<EditText>(Resource.Id.text2);
     }

   private void  recieved()
   {
    text2.Text = "mesage";   

    }
 }
1 голос
/ 05 октября 2011

text2 определено внутри OnCreate, поэтому received ничего не знает об этом.

Вам нужно определить text2 как поле класса, например:

public class MainChat : Activity
{
    private EditText text2;
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
         text2 = FindViewById<EditText>(Resource.Id.text2);
     }

   private void  recieved()
   {
    text2.Text = "mesage";   // The text2 does not existe in this context 

    }
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...