2015-06-25 67 views
0

對不起,如果這是this one重複問題,我已經嘗試過提供的解決方案和其他一些帖子,但他們都沒有爲我工作。如何使EditText可點擊?

我畫了的EditText,但它是不可點擊(無鍵盤出現觸摸後)

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback{ 
private LinearLayout lL; 
private EditText editText; 
@Override 
public void surfaceCreated(SurfaceHolder holder){ 
    lL = new LinearLayout(getContext()); 

    editText = new EditText(getContext()); 

    editText.setFocusableInTouchMode(true); 
    editText.setClickable(true); 
    editText.requestFocus(); 

    editText.setText("Hello World"); 
    editText.setVisibility(View.VISIBLE); 
    lL.addView(editText); 
    lL.setDrawingCacheEnabled(true); 
    lL.measure(canvas.getWidth(), canvas.getHeight()); 
    lL.layout(0, 0, canvas.getWidth(), canvas.getHeight()); 
    lL.bringChildToFront(editTextView); 
} 

@Override 
public void draw(Canvas canvas){ 

    canvas.drawBitmap(lL.getDrawingCache(),50,50,null); 
} 
} 

我也試過這個從我看到的解決方案:(使用XML創建的EditText)

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

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
</RelativeLayout> 

變化的EditText此,除去canvas.drawBitmap():

EditText editText = (EditText)findViewById(R.id.editText1); 

但噸當我這樣做時,他的應用總是強制關閉。

主要問題是如何使EditText上點擊和展示軟鍵盤一樣,當你只是把它從XML中沒有繪製在帆布上,在所有

+0

不要在您的繪畫方法中實現視圖!當視圖或視圖的一部分無效時,總是調用繪製。 在自定義視圖的構造函數中創建佈局並添加它。 – Fabian

+0

你可能想用這個editText2 =(EditText)findViewById(R.id.editText2); firstEText.setOnFocusChangeListener(新View.OnFocusChangeListener(){ @覆蓋 公共無效onFocusChange(查看V,布爾hasFocus){ 如果(hasFocus){// TODO:東西 }} }); – ASP

+0

不要用draw方法初始化視圖,並且可以發佈logcat堆棧跟蹤嗎? – RajSharma

回答

0

您可以設置佈局的屬性一樣android:descendantFocusability="beforeDescendants"android:focusableInTouchMode="enter code here"

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

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"/> 
</RelativeLayout> 

願這一個有益的;)

How to make EditText not focused when creating Activity

+0

恩,請你解釋一下這個目的是什麼?我剛剛設定,沒有任何改變。 – Rawr

+0

編輯文本沒有關注 –

+0

我輸入了android:descendantFocusability =「beforeDescendants」和android:focusableInTouchMode =「true」並且嘗試了「false」,但他們都沒有做任何更改。我不知道爲什麼 – Rawr

0

通過DEFA ult,如果你點擊edittext,鍵盤將會顯示,除非你做了一些改變行爲。不需要在xml佈局中設置可點擊的,可調焦的。只需創建一個編輯文字&使用它。

0

,如果你要使用編輯文本,以打開像您可以使用Android日期選擇器對話框:可調焦= FLASE因此點擊編輯文本將打開對話框等

希望這將有助於。