2016-03-08 121 views
4

我有一個textView和一個EditText和一個按鈕的佈局。 EditText被製作爲不可編輯的xml文件。我想通過代碼啓用它。以編程方式編輯EditText

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Name" 
     android:id="@+id/textView12" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/etName" 
     android:layout_below="@+id/textView12" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:editable="false" 
     /> 
    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@drawable/edit" 
     android:onClick="edit" 
     android:id="@+id/btnName" 
     android:layout_centerVertical="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 
+0

喜歡'editText.setEditable(true);' – ELITE

+0

你試過setFocusable或setEnabled嗎? –

+0

http://stackoverflow.com/questions/660151/how-to-replicate-androideditable-false-in-code –

回答

8

刪除android:editable="false"這條線從機器人XML文件作爲該方法在機器人棄用。並使用下面的代碼來做到這一點。

  • 使用Java代碼

EditText不可編輯在活動onCreate後執行下列setContentView()方法

EditText text = (EditText) findViewById(R.id.etName); 
text.setTag(text.getKeyListener()); 
text.setKeyListener(null); 
  • 它可編輯的使用下面的代碼使

    text.setKeyListener((KeyListener) textView.getTag()); 
    

希望能對大家有所幫助。

+0

它的工作謝謝精英:) – Krishna

+0

但在edittext –

+0

什麼是crashtracttrace? – ELITE

0

的哎代替編輯使用android:enabled="false"和在代碼setEnabled(true)

+0

讓我知道反饋 –

+0

嗨Naveen,謝謝你的寶貴意見。但你的建議不會按照我的要求工作。我希望edittext不能初始化編輯,只需點擊一個Imagebutton就可以編輯。根據你的建議,最初它會在加載時自行編輯。 – Krishna

+0

嘗試使用此組合一次,android:enabled =「false」android:focusable =「false」並且在代碼中setEnabled(true) –