2013-11-27 49 views
0

我已經制作了一個簡單的按鈕,它在EditText中添加數字「1」。EditText使用按鈕添加文本時水平滾動

下面是XML代碼

<EditText 
    android:id="@+id/edittext1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="1" 
    android:onClick="Btn1" 
/> 

和Java代碼

public void Btn1(View v){ 
    EditText edittext1 = (EditText) findViewById(R.id.edittext1); 
    String getText = edittext1.getText().toString(); 
    show_results.setText(getText+"1"); 
} 

當我在寫EditText與鍵盤,它會自動滾動水平和顯示的最後一個字符。但是當我按下按鈕時,它會添加文本,但不會滾動(所以只有第一個字符可見)。

有沒有解決方案?

+0

要滾動查看? – Naddy

+0

@Naddy它不是一個視圖。這是EditText – Enve

+0

改正你的基本隊友。 EditText擴展了擴展View的TextView。無論如何,你想滾動EditText?這對我來說不是很清楚。 – Naddy

回答

1

試試這個:

public void Btn1(View v){ 
    EditText edittext1 = (EditText) findViewById(R.id.edittext1); 
    String getText = edittext1.getText().toString(); 
    show_results.setText(getText+"1"); // not really sure what show_results is, 
             // but I suppose it's another EditText 
    show_results.setSelection(edittext1.getText().length()); 
} 
+0

這段代碼應該做什麼? – Enve

+0

'setSelection':http://developer.android.com/reference/android/widget/EditText.html#setSelection(int)它導致http://developer.android.com/reference/android/text/Selection。 html#setSelection(android.text.Spannable,int)其中聲明'將光標移動到偏移索引。' - 我可能已經很瞭解你的問題,但我希望你的光標在插入文本的末尾(即使你的編輯文本的高度是固定的,視圖將始終滾動 - 我編輯的答案是模式特定的) – Spiri