3

我最近开始使用 Android。我对Android没有太多经验。这是我第一次使用 Android。我需要将屏幕分成两半,然后执行一些任务。

  1. 将屏幕一分为二后,在下半部分(表示下半部分),我需要创建一个名为Latitudethen 的标签,该标签对应于Textbox将显示当前纬度值的 a 和另一个命名Longitude并对应于另一个标签Textbox并将显示当前的标签经度值。

我开始研究这个,并且取得了一些进展——下面是我目前用来将屏幕分成两半的 XML 文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.8"
        android:background="#000000" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#000000" >

        <!-- Latitude Label -->

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Latitude"
            android:textColor="#372c24" />

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dip"
            android:layout_marginTop="5dip"
            android:singleLine="true" />
        <!-- Longitude Label -->

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Longitude"
            android:textColor="#372c24" />

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dip"
            android:password="true"
            android:singleLine="true" />
    </LinearLayout>

</LinearLayout>

我的问题是-

  1. 使用上述 XML 文件后,我无法在 Android 屏幕上正确看到我的both label和,我的 XML 文件中是否缺少任何内容?both textbox
  2. 其次,如何在相应的文本框中显示当前的经纬度值。

任何帮助将不胜感激,因为我是 Android 世界的新手。

更新:-下面是我在油漆中制作的图表,我想要这样的东西。在此处输入图像描述

如果你看屏幕,它被分成两部分,在下半部分,有两个标签(纬度和经度),应该是这样的,每个标签前面都有一个文本框保存相应的纬度和经度值。

更新代码:-对于当前位置-

public class MainActivity extends Activity implements LocationListener {
    private TextView latituteField;
    private TextView longitudeField;
    private LocationManager locationManager;
    private String provider;

    private static final String TAG = "CurrentLocation";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        latituteField = (TextView) findViewById(R.id.lat1);
        longitudeField = (TextView) findViewById(R.id.lat2);

     // Get the location manager
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the location provider -> use
        // default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        locationManager.requestLocationUpdates(provider, 0, 0, this);
        Location location = locationManager.getLastKnownLocation(provider);
        onLocationChanged(location);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        locationManager.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location) {
       if (location != null) {
           System.out.println("Provider " + provider + " has been selected.");
           int lat = (int) (location.getLatitude());
           int lng = (int) (location.getLongitude());
           Log.v(TAG, lat+ " "+ lng);
           latituteField.setText(String.valueOf(lat));
           longitudeField.setText(String.valueOf(lng));
       } else {
           latituteField.setText("Provider not available");
           longitudeField.setText("Provider not available");
       }
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }
}

但是下面的代码没有在相应的文本框中显示任何纬度或经度值?我做错了什么吗?

4

1 回答 1

3

“使用上述 XML 文件后,我无法在 Android 屏幕上正确看到我的两个标签和两个文本框,我的 XML 文件中是否缺少任何内容?”

看来,您的LinearLayout标签可能弄乱了。

看看这是否更接近您正在寻找的内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@android:color/black" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <!-- currently empty -->

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <!-- Latitude Label -->

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Latitude"
                android:textColor="#372c24" />

            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dip"
                android:layout_marginTop="5dip"
                android:layout_weight="0.35"
                android:singleLine="true"
                android:text="test1" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <!-- Longitude Label -->

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Longitude"
                android:textColor="#372c24" />

            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dip"
                android:layout_weight="0.35"
                android:password="true"
                android:singleLine="true"
                android:text="test2" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

图片

在此处输入图像描述


“其次,如何在相应的文本框中显示当前的经纬度值。”

你有什么尝试?

您需要获取当前的纬度/经度。然后只需将文本设置在EditText.

于 2012-07-19T04:10:21.943 回答