How to get android default attributes in a custom view

血红的双手。 提交于 2020-03-21 17:57:28

问题


I have created a simple custom view that contains a RelativeLayout and an 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>

Also I have added some custom attributes in res/values/attrs.xml and I retrieve these attributes in my custom view constructor and everything works ok.

Now, I want to retrieve EditText default attributes in my custom view for example I want to get android:text attribute in my custom view.

My custom view class (simplified):

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

How can I do that without redeclaring text attribute in res/values/attrs.xml?


回答1:


You can add android:text to your declared syleable. But be sure to not redeclare it.

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

And then get this value from the style like you would with any other of your attributes with the index of R.styleable.CustomEditText_android_text.

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



回答2:


I think you cannot do that. Add an id to your edittext in xml and if you want to retrieve value of editText, simply use :

edittext.getText().toString()



回答3:


I think it's not possible because your customview extends relativeLayout and it doesnt have this attribute. You can access android:text or other attributes if you extend from EditText or TextView directly.




回答4:


You can build this using layout like

<?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>

Your customview class will be like this

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


来源:https://stackoverflow.com/questions/44584296/how-to-get-android-default-attributes-in-a-custom-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!