如何

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);