2016-09-07 100 views
0

當以編程方式創建一個EditText通過它的構造函數的EditText(上下文的背景下),它會創建一個與舊的Android設計:如何以編程方式創建材質設計EditText?

enter image description here

它不允許文本選擇,(但我能移動光標)。如何使一個EditText與較新的設計,就像這樣:

enter image description here

我已經找到了如何刪除橙色邊框(即setBackgroundColor),但我不能找到一種方法如何改變光標設計和如何使選擇文本(即我的意思是看到左右文本選擇句柄)。我需要以編程方式完成所有這些,因爲我在Delphi中快速創建EditText(所以不需要xml)。


按照要求,這是與我的應用程序而來的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<!-- BEGIN_INCLUDE(manifest) --> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.myapp.app" 
     android:versionCode="1" 
     android:versionName="1.0.0" 
     android:installLocation="preferExternal"> 

    <!-- This is the platform API where NativeActivity was introduced. --> 
    <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.CALL_PHONE" /> 
    <uses-permission android:name="android.permission.CAMERA" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.READ_CALENDAR" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_CALENDAR" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

    <uses-feature android:glEsVersion="0x00020000" android:required="True"/> 
    <application android:persistent="False" 
     android:restoreAnyVersion="False" 
     android:label="myapp" 
     android:debuggable="True" 
     android:largeHeap="False" 
     android:icon="@drawable/ic_launcher" 
     android:theme="@android:style/Theme.NoTitleBar" 
     android:hardwareAccelerated="true"> 



     <!-- Our activity is a subclass of the built-in NativeActivity framework class. 
      This will take care of integrating with our NDK code. --> 
     <activity android:name="com.embarcadero.firemonkey.FMXNativeActivity" 
       android:label="myapp" 
       android:configChanges="orientation|keyboard|keyboardHidden|screenSize" 
       android:launchMode="singleTask"> 
      <!-- Tell NativeActivity the name of our .so --> 
      <meta-data android:name="android.app.lib_name" 
       android:value="myapp" /> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver android:name="com.embarcadero.rtl.notifications.NotificationAlarm" /> 

    </application> 
</manifest> 
<!-- END_INCLUDE(manifest) --> 
+0

大部分部件的時間樣式的延長AppCompatActivity從在AndroidManifest應用標籤主題繼承。你可以發佈AndroidManifest.xml文件嗎? – jakubbialkowski

+0

所以你想要自定義光標像黑色光標,綠色光標如果我沒有錯 – Bhavnik

+0

@jakubbialkowski我只是發佈我的AndroidManifest.xml的內容。但它不只是設計,現在我甚至不能在我的編輯文本中選擇文本:( – loki

回答

0

什麼你想要做的就是建立在你的佈局文件夾一個EditText XML,你可以重複使用,並且可以在那裏指定樣式。

<?xml version="1.0" encoding="utf-8"?> 
<EditText xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
style="SUPPLY YOUR STYLE HERE FROM STYLES.XML" /> 

你也可以告訴它使用一個主題。然後,你吹這樣的:

EditText myEditText = (EditText)getLayoutInflater().inflate(R.layout.SOME_LAYOUT, null); 

爲了使您的文本選擇,你可以試試這個:

editText.setTextIsSelectable(true);

+0

感謝VicVu,但它如何工作,如果現在我甚至不能選擇任何文本(我只能看到中間文本句柄選擇器,但不是左邊或右邊的文本句柄選擇器)?我也想避免使用任何XML – loki

+0

請參閱我的編輯。 – VicVu

+0

感謝vicVU的支持,但不幸的是setTextIsSelectable沒有幫助,我仍然無法看到右側和左側文本選擇句柄:(我只能看到中間文本選擇句柄.. – loki

-1

使用構造函數的3個參數的版本,並通過在你想要的風格。

+0

但我無法理解如何使用它EditText(上下文上下文,AttributeSet attrs,int defStyleAttr)...你有一些示例?在AttributeSet和defStyleAttr中放什麼? – loki

+0

屬性設置它是一組屬性值,基本上所有的android:xml第三個參數是風格 –

0

Screen shot of text selection is here嘿洛基, 試試這個,可能會對你有所幫助..... 在Java文件輸入以下代碼片段

try { 
     Field f = TextView.class.getDeclaredField("mCursorDrawableRes"); 
     f.setAccessible(true); 
     f.set(editEmail, R.drawable.cursor_black); // set your programmatically generated edittext in this method 
    } catch (NoSuchFieldException e) { 
     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } 

在繪製文件夾中創建cursor_black.xml。

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<size android:width="2px" /> 
<solid android:color="@color/black" /> 
</shape> 

而且還嘗試在你的活動類