如何

0

我已經創建了一個包含RelativeLayout一個簡單的自定義視圖自定義視圖得到的Android默認屬性和EditText如何

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

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

而且我已經添加在res/values/attrs.xml一些自定義的屬性和我檢索這些屬性我的自定義視圖構造函數和一切正常。

現在,我想在我的自定義視圖中檢索EditText默認屬性,例如我想在我的自定義視圖中獲取android:text屬性。

我的自定義視圖類(簡化):

public class CustomEditText extends RelativeLayout { 
    private int panCount; 

    public CustomEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, 
      R.styleable.CustomEditText, 0, 0); 
     try { 
      this.panCount = typedArray.getInt(R.styleable.CustomEditText_panCount, 16); 
     } finally { 
      typedArray.recycle(); 
     } 
    } 
} 

我怎麼能做到這一點,而不在res/values/attrs.xml重新聲明文本屬性?

+0

它是混淆你不使用你的觀點在XML代碼...!你確定你已經發布了最新的代碼。檢查這個答案:https://stackoverflow.com/a/8090772/7134908它可能會解決您的問題,在xml中設置值,並進入Java vode –

+0

你可以添加'Customable文件''CustomEditText'? – tynn

+1

[如何在自定義TextView中使用命名空間「android」獲取屬性的可能的重複](https://stackoverflow.com/questions/26486791/how-to-get-attributes-with-namespace-android-in-custom-textview ) –

回答

0

我認爲你不能那樣做。 的ID添加到您的EditText在XML中,如果你想檢索EDITTEXT的價值,簡單地使用:

edittext.getText().toString() 
+0

我想在xml中使用android:text =「some text」並在自定義視圖類中像檢索自定義屬性那樣檢索它 – hosseinAmini

+0

請發佈您的自定義視圖類 –

+0

我添加了自定義視圖類 – hosseinAmini

-1

我想是因爲你的customview延伸RelativeLayout的和它沒有這個屬性是不可能的。如果您直接從EditText或TextView擴展,則可以訪問android:text或其他屬性。

0

您可以建立這個使用佈局像

<?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/edittext" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:text="My Custom View" /> 
</RelativeLayout> 

你customview類會是這樣

public class CustomEditText extends RelativeLayout { 
    LayoutInflater mInflater; 

    public CustomView(Context context) { 
     super(context); 
     mInflater = LayoutInflater.from(context); 
     init(); 

    } 

    public CustomView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     mInflater = LayoutInflater.from(context); 
     init(); 
    } 

    public CustomView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mInflater = LayoutInflater.from(context); 
     init(); 
    } 

    // Here you can access your edittext text 
    public void init() { 
     View v = mInflater.inflate(R.layout.custom_view, this, true); 
     EditText et = (TextView) v.findViewById(R.id.edittext); 
     et.setText(" Custom RelativeLayout"); 
    } 
} 
1

您可以添加android:text您宣佈syleable。但一定不要重新聲明它。

<declare-styleable name="CustomEditText"> 
    <attr name="android:text" /> 
</declare-styleable> 

,然後得到從風格就像你使用任何其他與R.styleable.CustomEditText_android_text索引你的屬性的這個值。

CharSequence text = typedArray.getText(R.styleable.CustomEditText_android_text);