2012-12-13 43 views
1

這應該很簡單,但我已經嘗試了關於這個主題的一堆東西,並且它們或者什麼都不做或者崩潰。如何獲取顯示文本右側的初始光標位置

我有一個edittext字段,從最後一個條目開始,比如「12」,我想讓初始光標位於2的右邊,它總是從1的左邊開始。那樣會很好,如果有一個del鍵,但鍵盤上只有一個後退鍵,用戶必須手動重新定位光標,然後再輸入舊文本。有幾個領域,他很可能想要讓大部分領域獨自一人。如果他想編輯第二個字段讓他擊中「下一個」「bs」,「bs」,「45」,「下一個」,「完成」,我想要什麼。這樣他不必將光標放在所有位置。

------------ UPDATE ----------------------

這裏是主文件

package com.example.test; 
import android.os.Bundle; 
import android.app.Activity; 
import android.widget.EditText; 


public class MainActivity extends Activity { 
    static EditText edit; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     edit = (EditText) findViewById(R.id.edit); 
     edit.setSelection(edit.getText().toString().length()); 
    } 
} 

這裏是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" 
tools:context=".MainActivity" > 

<EditText 
    android:id="@+id/edit" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/textView2" 
    android:layout_alignParentLeft="true" 
    android:layout_marginBottom="60dp" 
    android:text="12" 
    android:nextFocusDown="@+id/textView4" 
    android:inputType="number" 
    android:ellipsize="end" 
    android:gravity="right" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
    android:id="@+id/textView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/edit" 
    android:layout_alignBottom="@+id/edit" 
    android:layout_toRightOf="@+id/edit" 
    android:text=":" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<EditText 
    android:id="@+id/textView4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBaseline="@+id/textView3" 
    android:layout_alignBottom="@+id/textView3" 
    android:layout_toRightOf="@+id/textView3" 
    android:text="00" 
    android:inputType="number" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

回答

2

每個使用的EditText在setSelection()onCreate()

edit.setSelection(edit.getText().toString().length()); 
edit2.setSelection(edit2.getText().toString().length()); 

遊標仍然會尊重用戶的觸摸手勢,但軌跡球和其他移動方法將以右側的光標開始。

+0

它崩潰。我上面發佈了我的代碼。 –

+0

當然,它崩潰了,你忘了用'edit =(EditText)findViewById(R.id.edit);':)初始化'edit'並且我不認爲編輯應該是靜態的,因爲它不是靜態的在這個班級以外使用。 – Sam

+0

我修改了上面的代碼以包含缺少的代碼(至少我認爲它現在放置正確),但它仍然崩潰。 :-( –

相關問題