我想在点击按钮时自动填充编辑文本控件。这是我的代码。这是android应用程序

我想在点击按钮时自动填充编辑文本控件。这是我的代码。这是android应用程序,android,visual-studio,xamarin,Android,Visual Studio,Xamarin,我的布局页面是: 这是我的布局页面。单击按钮时,我希望在编辑文本控件中打印纬度和经度值。我的活动方法有getLocation方法,我正在使用Location services API。在调试期间以及在我的选项卡上运行应用程序时,我没有看到任何错误 > <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

我的布局页面是:

这是我的布局页面。单击按钮时,我希望在编辑文本控件中打印纬度和经度值。我的活动方法有getLocation方法,我正在使用Location services API。在调试期间以及在我的选项卡上运行应用程序时,我没有看到任何错误

> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearLayout1"
    android:background="#000000"
    android:minWidth="25px"
    android:minHeight="25px"
    android:weightSum="100"
    tools:actionBarNavMode="list">
    <TextView
        android:text="Well Name:"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/WellNameLabel"
        android:layout_weight="3.5"
        android:textColor="#00BFFF" />
    <EditText
        android:id="@+id/WellText"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5"
        android:textColor="#000000"
        android:layoutDirection="inherit"
        android:backgroundTint="#b4b4b4ff" 
        android:hint="Well Name"/>
    <TextView
        android:text="Address"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3.5"
        android:textColor="#00BFFF"
        android:id="@+id/Address" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5"
        android:textColor="#000000"
        android:backgroundTint="#b4b4b4ff"
        android:id="@+id/EditAddress" 
        android:hint="Address"/>
    <TextView
        android:text="City"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3.5"
        android:textColor="#00BFFF"
        android:id="@+id/CityL" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5"
        android:textColor="#000000"
        android:backgroundTint="#b4b4b4ff"
        android:id="@+id/CityText"
        android:hint="State"/>
    <TextView
        android:text="State"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3.5"
        android:textColor="#00BFFF"
        android:id="@+id/StateLable" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5"
        android:textColor="#FFFFFF"
        android:backgroundTint="#b4b4b4ff"
        android:id="@+id/StateText"
        android:hint="State"/>
    <TextView
        android:text="Zip:"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3.5"
        android:textColor="#00BFFF"
        android:id="@+id/Zip" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5"
        android:textColor="#00BFFF"
        android:backgroundTint="#b4b4b4ff"
        android:id="@+id/ZipText"
        android:hint="Zip"/>
    <TextView
        android:text="Country:"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3.5"
        android:textColor="#00BFFF"
        android:id="@+id/CountryLabel" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5"
        android:textColor="#FFFFFF"
        android:backgroundTint="#b4b4b4ff"
        android:id="@+id/TextCountry"
        android:hint="Country"/>
    <TextView
        android:text="Latitude"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3.5"
        android:textColor="#00BFFF"
        android:id="@+id/LatitudeLabel" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5"
        android:textColor="#FFFFFF"
        android:backgroundTint="#b4b4b4ff"
        android:id="@+id/TextLatitude"
        android:autoText="true" 
        android:hint="Latitude"/>
    <TextView
        android:text="Longitude"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3.5"
        android:textColor="#00BFFF"
        android:id="@+id/LongitudeLabel" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5"
        android:textColor="#FFFFFF"
        android:backgroundTint="#b4b4b4ff"
        android:id="@+id/TextLongitude"
        android:autoText="true"
        android:hint="Longitude"
        android:editable="true" />
    <Button
        android:text="GetLocation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/GetLocation" />
</LinearLayout>`<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearLayout1"
    android:background="#000000"
    android:minWidth="25px"
    android:minHeight="25px"
    android:weightSum="100"
    tools:actionBarNavMode="list">
    <TextView
        android:text="Well Name:"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/WellNameLabel"
        android:layout_weight="3.5"
        android:textColor="#00BFFF" />
    <EditText
        android:id="@+id/WellText"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5"
        android:textColor="#000000"
        android:layoutDirection="inherit"
        android:backgroundTint="#b4b4b4ff" 
        android:hint="Well Name"/>
    <TextView
        android:text="Address"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3.5"
        android:textColor="#00BFFF"
        android:id="@+id/Address" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5"
        android:textColor="#000000"
        android:backgroundTint="#b4b4b4ff"
        android:id="@+id/EditAddress" 
        android:hint="Address"/>
    <TextView
        android:text="City"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3.5"
        android:textColor="#00BFFF"
        android:id="@+id/CityL" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5"
        android:textColor="#000000"
        android:backgroundTint="#b4b4b4ff"
        android:id="@+id/CityText"
        android:hint="State"/>
    <TextView
        android:text="State"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3.5"
        android:textColor="#00BFFF"
        android:id="@+id/StateLable" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5"
        android:textColor="#FFFFFF"
        android:backgroundTint="#b4b4b4ff"
        android:id="@+id/StateText"
        android:hint="State"/>
    <TextView
        android:text="Zip:"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3.5"
        android:textColor="#00BFFF"
        android:id="@+id/Zip" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5"
        android:textColor="#00BFFF"
        android:backgroundTint="#b4b4b4ff"
        android:id="@+id/ZipText"
        android:hint="Zip"/>
    <TextView
        android:text="Country:"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3.5"
        android:textColor="#00BFFF"
        android:id="@+id/CountryLabel" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5"
        android:textColor="#FFFFFF"
        android:backgroundTint="#b4b4b4ff"
        android:id="@+id/TextCountry"
        android:hint="Country"/>
    <TextView
        android:text="Latitude"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3.5"
        android:textColor="#00BFFF"
        android:id="@+id/LatitudeLabel" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5"
        android:textColor="#FFFFFF"
        android:backgroundTint="#b4b4b4ff"
        android:id="@+id/TextLatitude"
        android:autoText="true" 
        android:hint="Latitude"/>
    <TextView
        android:text="Longitude"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3.5"
        android:textColor="#00BFFF"
        android:id="@+id/LongitudeLabel" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="7.5"
        android:textColor="#FFFFFF"
        android:backgroundTint="#b4b4b4ff"
        android:id="@+id/TextLongitude"
        android:autoText="true"
        android:hint="Longitude"
        android:editable="true" />
    <Button
        android:text="GetLocation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/GetLocation" />
</LinearLayout>`
>
`
`
我的Acitvity页面如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

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

    namespace HydrologyHeck
    {
    [Activity(Label = "WellInfo")]
    public class WellInfo : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.WellReadings);
            // Create your application here
        }
        public void GetLocation(Location location)
        {
            var Latitude = FindViewById<EditText>(Resource.Id.TextLatitude);
            Latitude.Text = location.Latitude.ToString();
            var Longitude = FindViewById<EditText>(Resource.Id.TextLongitude);
            Longitude.Text = location.Longitude.ToString();
            //var geocoder = new Geocoder(this);
            //var Address = FindViewById<EditText>(Resource.Id.Address);
            //Address.Text = geocoder.GetFromLocation(location.Latitude, location.Longitude, 1).ToString();
            //Toast.MakeText(this,"The address is"+ Address.Text, ToastLength.Short).Show();

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Android.Locations;
名称空间HydrologyHeck
{
[活动(Label=“WellInfo”)]
公共课堂信息:活动
{
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.WellReaders);
//在此处创建应用程序
}
公共位置(位置)
{
var Latitude=FindViewById(Resource.Id.TextLatitude);
Latitude.Text=location.Latitude.ToString();
var Longitude=findviewbyd(Resource.Id.TextLongitude);
Longitude.Text=location.Longitude.ToString();
//var geocoder=新的地理编码器(本);
//var Address=findviewbyd(Resource.Id.Address);
//Address.Text=geocoder.GetFromLocation(location.Latitude,location.Longitude,1.ToString();
//Toast.MakeText(这个“地址是”+address.Text,ToastLength.Short).Show();
}
}
}

提前感谢大家

您应该在活动中添加Ilocation侦听器,并在OnLocationChanged事件中更改EditText值,有关详细信息,请查看此处

您还可以使用以下示例代码:

LocationManager locMgr;
    string tag = "MainActivity";

    public override void OnStart ()
    {
        base.OnStart ();
        Log.Debug (tag, "OnStart called");
    }
    public override void OnResume ()
    {
        base.OnResume (); 
        Log.Debug (tag, "OnResume called");

        // initialize location manager
        locMgr = Activity.GetSystemService (Context.LocationService) as LocationManager;

        string Provider = LocationManager.GpsProvider;

        //if (locationProvider != null) {
        if (locMgr.IsProviderEnabled(Provider)) {
            locMgr.RequestLocationUpdates (Provider, 2000, 1, this);
        } else {
            Log.Info (tag, "No location providers available");              
        }
    }

    public override void OnPause ()
    {
        base.OnPause ();

        // RemoveUpdates takes a pending intent - here, we pass the current Activity
        locMgr.RemoveUpdates (this);
        Log.Debug (tag, "Location updates paused because application is entering the background");
    }
    public override void OnStop ()
    {
        base.OnStop ();
        Log.Debug (tag, "OnStop called");
    }

    public void OnLocationChanged (Android.Locations.Location location)
    {
        Log.Debug (tag, "Location changed");

        var currentCoordinates = new Coordinates (location.Latitude, location.Longitude);
        LoadData (currentCoordinates);
        Latitude.Text = "Latitude: " + location.Latitude.ToString();
        Longitude.Text  = "Longitude: " + location.Longitude.ToString();
    }
    public void OnProviderDisabled (string provider)
    {
        Log.Debug (tag, provider + " disabled by user");
    }
    public void OnProviderEnabled (string provider)
    {
        Log.Debug (tag, provider + " enabled by user");
    }
    public void OnStatusChanged (string provider, Availability status, Bundle extras)
    {
        Log.Debug (tag, provider + " availability has changed to " + status.ToString());
    }

您应该在活动中添加Ilocation侦听器,并在OnLocationChanged事件中更改EditText值。有关更多详细信息,请查看此处

您还可以使用以下示例代码:

LocationManager locMgr;
    string tag = "MainActivity";

    public override void OnStart ()
    {
        base.OnStart ();
        Log.Debug (tag, "OnStart called");
    }
    public override void OnResume ()
    {
        base.OnResume (); 
        Log.Debug (tag, "OnResume called");

        // initialize location manager
        locMgr = Activity.GetSystemService (Context.LocationService) as LocationManager;

        string Provider = LocationManager.GpsProvider;

        //if (locationProvider != null) {
        if (locMgr.IsProviderEnabled(Provider)) {
            locMgr.RequestLocationUpdates (Provider, 2000, 1, this);
        } else {
            Log.Info (tag, "No location providers available");              
        }
    }

    public override void OnPause ()
    {
        base.OnPause ();

        // RemoveUpdates takes a pending intent - here, we pass the current Activity
        locMgr.RemoveUpdates (this);
        Log.Debug (tag, "Location updates paused because application is entering the background");
    }
    public override void OnStop ()
    {
        base.OnStop ();
        Log.Debug (tag, "OnStop called");
    }

    public void OnLocationChanged (Android.Locations.Location location)
    {
        Log.Debug (tag, "Location changed");

        var currentCoordinates = new Coordinates (location.Latitude, location.Longitude);
        LoadData (currentCoordinates);
        Latitude.Text = "Latitude: " + location.Latitude.ToString();
        Longitude.Text  = "Longitude: " + location.Longitude.ToString();
    }
    public void OnProviderDisabled (string provider)
    {
        Log.Debug (tag, provider + " disabled by user");
    }
    public void OnProviderEnabled (string provider)
    {
        Log.Debug (tag, provider + " enabled by user");
    }
    public void OnStatusChanged (string provider, Availability status, Bundle extras)
    {
        Log.Debug (tag, provider + " availability has changed to " + status.ToString());
    }

我试着写代码。locMgr=Activity.GetSystemService(Context.LocationService)作为LocationManager;我得到这个“错误5非静态字段、方法或属性'Android.Content.Context.GetSystemService(string)'需要对象引用”我的代码示例是片段,在您的情况下,应该使用locMgr=GetSystemService(Context.LocationService)作为LocationManager;我尝试将代码.locMgr=Activity.GetSystemService(Context.LocationService)编写为LocationManager;我得到了这样一个“错误5非静态字段、方法或属性'Android.Content.Context.GetSystemService(string)'需要对象引用”我的代码示例是针对片段的,在您的情况下,应该使用locMgr=GetSystemService(Context.LocationService)作为LocationManager;