android gps距离。 EditText仅字母表验证

时间:2014-01-28 11:51:59

标签: android gps android-edittext

我的应用程序有两个功能,验证用户输入的输入。一个EditText必须只是数字,一个必须是仅字母。没有空格或特殊字符。我已完成数字,但我搜索没有字母表验证。有人可以教我如何循环使用EditText并确保每个字符都是字母吗? 编辑:我在afterTextChanged中更改了我的代码。当我在userTextInout中按下字母或数字时,我的应用程序崩溃了 第二是通过使用gps来计算用户已采取的步数或距离。但是我的输出什么都没有。它什么都没显示。我不确定它是否与模拟器有关。在我以前的应用程序中,当我点击模拟器控件发送gps数据时,它不会发送大部分时间,在我反复点击发送按钮后,它可以更新我的位置。

我听说有一个新的Location API可以提高准确性,但是和我的有什么区别?在Google开发者页面上没有太多细节。

即使我计算了距离,我该怎么显示它? setText不会显示很长时间。这是我第一次使用distanceBetween方法,所以我不熟悉它。

XML代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/textInput"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="58dp"
    android:layout_marginTop="34dp"
    android:ems="5" 
    android:maxLength="100"
    android:inputType="textMultiLine" />


<EditText
    android:id="@+id/numberInput"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textInput"
    android:layout_below="@+id/textInput"
    android:layout_marginTop="64dp"
    android:ems="5"
    android:inputType="number" 
    android:maxLength="3" />

<TextView
    android:id="@+id/Distance"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/numberInput"
    android:layout_marginLeft="37dp"
    android:layout_marginTop="107dp"
    android:text="TextView" />

<TextView
    android:id="@+id/currentLat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/Distance"
    android:layout_below="@+id/Distance"
    android:layout_marginTop="29dp"
    android:text="TextView" />

<TextView
    android:id="@+id/currentLon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/currentLat"
    android:layout_below="@+id/currentLat"
    android:layout_marginTop="29dp"
    android:text="TextView" />

</RelativeLayout>

主要java代码

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
//import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements TextWatcher{

protected LocationManager locationManager;
EditText userNumberInput;
EditText userTextInput;
TextView distanceText;
TextView latitude;
TextView longitude;
double lat1,lon1,lat2,lon2;
long dist;
float[] result;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    userNumberInput=(EditText)findViewById(R.id.numberInput);
    userNumberInput.addTextChangedListener(this);
    userTextInput=(EditText)findViewById(R.id.textInput);
    distanceText=(TextView)findViewById(R.id.Distance);
    latitude=(TextView)findViewById(R.id.currentLat);
    longitude=(TextView)findViewById(R.id.currentLon);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);


    Location loc1=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    Criteria criteria = new Criteria();

    //Get the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);
    locationManager.requestLocationUpdates(provider, 0, 1, myLocationListener);

    latitude.setText(ToString(loc1.getLatitude()));
    longitude.setText(ToString(loc1.getLongitude()));

    //locationManager.requestLocationUpdates(netprovider, 0, 1, myLocationListener);

}

private CharSequence ToString(double latitude2) {
    // TODO Auto-generated method stub
    return null;
}

LocationListener myLocationListener = new LocationListener() 
{
    public void onLocationChanged(Location loc2) 
    {
        //Location loc2=new Location("");
        lat2=loc2.getLatitude();
        lon2=loc2.getLongitude();
        latitude.setText(ToString(loc2.getLatitude()));
        longitude.setText(ToString(loc2.getLongitude()));
        if(lat1 != 0 && lon1 != 0) { 
           Location.distanceBetween(lat1,lon1,lat2,lon2,result);
           dist+=(long)result[0];
        }
        lat1=lat2;
        lon1=lon2;
        lat2=loc2.getLatitude();
        lon2=loc2.getLongitude();


    distanceText.setText(lat1+","+lon1+","+lat2+","+lon2, dist);




}
private CharSequence ToString(double latitude) {
        // TODO Auto-generated method stub
        return null;
    }
public void onProviderDisabled(String provider)
{
    // Update application if provider disabled.
}
public void onProviderEnabled(String provider)
{
    // Update application if provider enabled.
}
public void onStatusChanged(String provider, int status,
        Bundle extras)
{
    // Update application if provider hardware status changed.
}
};




@Override
public void afterTextChanged(Editable edit) {
            String textFromEditView = edit.toString();
    try
    {
        boolean isOnlyAlphabet = textFromEditView.matches("/^[A-z]+$/");
        if(isOnlyAlphabet == false)
        {
            edit.replace(0, edit.length(), "only alphabets");
        }
    }
    catch(NumberFormatException e){}


}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
        int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}}

01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 70
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 70
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[1] is 70
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 70
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[1] is 70
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[2] is 41
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 77
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 77
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[1] is 65
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 77
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[1] is 65
01-28 21:33:00.730: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[2] is 69
01-28 21:33:00.740: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.740: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.740: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.740: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.765: D/libEGL(7383): loaded /system/lib/egl/libEGL_mali.so
01-28 21:33:00.765: D/libEGL(7383): loaded /system/lib/egl/libGLESv1_CM_mali.so
01-28 21:33:00.775: D/libEGL(7383): loaded /system/lib/egl/libGLESv2_mali.so
01-28 21:33:00.780: D/(7383): Device driver API match
01-28 21:33:00.780: D/(7383): Device driver API version: 10
01-28 21:33:00.780: D/(7383): User space API version: 10 
01-28 21:33:00.780: D/(7383): mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Tue Oct 16 15:37:13 KST 2012 
01-28 21:33:00.815: D/OpenGLRenderer(7383): Enabling debug mode 0
01-28 21:33:00.815: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.815: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.820: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.820: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.825: E/SensorManager(7383): thread start
01-28 21:33:00.825: D/SensorManager(7383): registerListener :: handle = 0  name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@424157f0
01-28 21:33:00.860: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.860: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.860: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.860: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:00.865: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 70
01-28 21:33:00.865: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 70
01-28 21:33:00.865: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[1] is 70
01-28 21:33:00.865: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 70
01-28 21:33:00.865: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[1] is 70
01-28 21:33:00.865: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[2] is 41
01-28 21:33:00.870: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 77
01-28 21:33:00.870: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 77
01-28 21:33:00.870: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[1] is 65
01-28 21:33:00.870: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[0] is 77
01-28 21:33:00.870: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[1] is 65
01-28 21:33:00.870: D/TextLayoutCache(7383): TextLayoutCache::replaceThai, prevBuffer[2] is 69
01-28 21:33:01.040: D/SensorManager(7383): unregisterListener::  Listener= android.view.OrientationEventListener$SensorEventListenerImpl@424157f0
01-28 21:33:01.040: D/Sensors(7383): Remain listener = Sending .. normal delay 200ms
01-28 21:33:01.040: I/Sensors(7383): sendDelay --- 200000000
01-28 21:33:01.045: D/SensorManager(7383): JNI - sendDelay
01-28 21:33:01.045: I/SensorManager(7383): Set normal delay = true
01-28 21:33:01.545: W/IInputConnectionWrapper(7383): showStatusIcon on inactive InputConnection
01-28 21:33:10.460: E/SensorManager(7383): thread start
01-28 21:33:10.465: D/SensorManager(7383): registerListener :: handle = 0  name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@424157f0
01-28 21:33:12.555: D/SensorManager(7383): unregisterListener::  Listener= android.view.OrientationEventListener$SensorEventListenerImpl@424157f0
01-28 21:33:12.555: D/Sensors(7383): Remain listener = Sending .. normal delay 200ms
01-28 21:33:12.555: I/Sensors(7383): sendDelay --- 200000000
01-28 21:33:12.555: D/SensorManager(7383): JNI - sendDelay
01-28 21:33:12.555: I/SensorManager(7383): Set normal delay = true
01-28 21:33:12.555: D/SensorManager(7383): registerListener :: handle = 0  name= LSM330DLC 3-axis Accelerometer delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@42417a08
01-28 21:33:12.565: W/IInputConnectionWrapper(7383): getSelectedText on inactive InputConnection
01-28 21:33:12.565: W/IInputConnectionWrapper(7383): setComposingText on inactive InputConnection
01-28 21:33:12.565: W/IInputConnectionWrapper(7383): getExtractedText on inactive InputConnection
01-28 21:33:12.675: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:12.675: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:12.680: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:12.680: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:13.260: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:13.260: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:13.260: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:13.260: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:13.645: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:13.645: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:13.645: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:13.645: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:15.225: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:15.225: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:15.225: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:15.230: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:15.845: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:15.845: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:15.845: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:15.845: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:16.425: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:16.425: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:16.430: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:16.430: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:18.260: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:18.260: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:18.260: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:18.260: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.695: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.695: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.695: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.695: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.860: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.860: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.860: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.865: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.880: D/dalvikvm(7383): GC_CONCURRENT freed 233K, 7% free 12329K/13191K, paused 13ms+4ms, total 41ms
01-28 21:33:19.995: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.995: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.995: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:19.995: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.245: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.245: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.245: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.245: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.395: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.395: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.395: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.400: E/Dynamiclayout(7383): java.lang.IndexOutOfBoundsException: charAt: 0 >= length 0
01-28 21:33:20.995: D/AndroidRuntime(7383): Shutting down VM
01-28 21:33:20.995: W/dalvikvm(7383): threadid=1: thread exiting with uncaught exception (group=0x4178f2a0)
01-28 21:33:20.995: E/AndroidRuntime(7383): FATAL EXCEPTION: main
01-28 21:33:20.995: E/AndroidRuntime(7383): java.lang.IllegalArgumentException: results is null or has length < 1
01-28 21:33:20.995: E/AndroidRuntime(7383):     at android.location.Location.distanceBetween(Location.java:394)
01-28 21:33:20.995: E/AndroidRuntime(7383):     at com.example.validationapp.MainActivity$1.onLocationChanged(MainActivity.java:74)
01-28 21:33:20.995: E/AndroidRuntime(7383):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:263)
01-28 21:33:20.995: E/AndroidRuntime(7383):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:196)
01-28 21:33:20.995: E/AndroidRuntime(7383):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:212)
01-28 21:33:20.995: E/AndroidRuntime(7383):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 21:33:20.995: E/AndroidRuntime(7383):     at android.os.Looper.loop(Looper.java:137)
01-28 21:33:20.995: E/AndroidRuntime(7383):     at android.app.ActivityThread.main(ActivityThread.java:4898)
01-28 21:33:20.995: E/AndroidRuntime(7383):     at java.lang.reflect.Method.invokeNative(Native Method)
01-28 21:33:20.995: E/AndroidRuntime(7383):     at java.lang.reflect.Method.invoke(Method.java:511)
01-28 21:33:20.995: E/AndroidRuntime(7383):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-28 21:33:20.995: E/AndroidRuntime(7383):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-28 21:33:20.995: E/AndroidRuntime(7383):     at dalvik.system.NativeStart.main(Native Method)
01-28 21:33:30.770: I/Process(7383): Sending signal. PID: 7383 SIG: 9

1 个答案:

答案 0 :(得分:1)

您可以使用正则表达式验证输入。只需使用此代码(在此处找到:Regular Expression to match only alphabetic characters):

boolean isOnlyAlphabet = userTextInput.matches("/^[A-z]+$/");

对于第二个问题,我无能为力。遗憾。