【问题标题】:Android findViewById() in Custom View自定义视图中的 Android findViewById()
【发布时间】:2013-09-01 17:36:53
【问题描述】:

我已经编写了我的自定义视图,我想在与我的自定义视图交互后更新一些其他视图。

主布局:

<?xml version="1.0" encoding="utf-8"?>

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

    <EditText
        android:id="@+id/display_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:layout_centerHorizontal="true"
        android:tag="name"
        android:ems="10" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/id_number"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/display_name"/>

    <ge.altasoft.custom_views.IdNumber
        android:id="@+id/custom_id_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/id_number"
        android:paddingLeft="35dip"
        custom:firstName="@id/display_name"/>
</RelativeLayout>

自定义视图布局:

<?xml version="1.0" encoding="utf-8"?>

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

    <EditText
        android:id="@+id/id_number_custom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:paddingRight="35dip" />

    <ImageButton
        android:id="@+id/load_data_button"
        android:layout_width="30dip"
        android:layout_height="30dip"
        android:layout_centerVertical="true"
        android:src="@drawable/load_data"
        android:layout_toRightOf="@id/id_number_custom" />

</RelativeLayout>

自定义视图类、构造函数和监听器:

 private int firstNameViewID;

public IdNumber (Context context, AttributeSet attrs) {
        super(context, attrs);
        initViews();

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IdNumber);
        final int N = a.getIndexCount();
        for(int i = 0; i < N; i++){
            int attr = a.getIndex(i);
            switch(attr){
                case R.styleable.IdNumber_firstName:
                    firstNameViewID = a.getResourceId(attr, -1);
                    break;
            }
        }
        a.recycle();
    }



 private void initViews() {
        inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.id_number_edit_text_custom, this, true);
        editText = (EditText) findViewById(R.id.id_number_custom);
        loadButton = (ImageButton) findViewById(R.id.load_data_button);
        loadButton.setVisibility(RelativeLayout.INVISIBLE);
        loadData();
    }

private void loadData(){
        loadButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText firstName = (EditText) findViewById(R.id.display_name);
                firstName.setText("Some Text");
            }
        });
    }

问题是EditText firstName = (EditText) findViewById(R.id.display_name); 返回null。 我知道只要调用findViewById() 就会在我膨胀的布局中搜索视图。

如果可能的话,如何从主布局中获取带有 id: display_name 的 EditText 视图?

提前致谢。

【问题讨论】:

  • 不,我不想获取自定义视图,我想从 mainLayout 获取其他视图,ID 为 diplay_name。
  • 您正在膨胀 xml,但没有存储返回的视图。您可以在该视图上按 id 查找视图。

标签: android android-layout view custom-attributes findviewbyid


【解决方案1】:

如下更改您的方法并检查它是否有效

private void initViews() {
    inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.id_number_edit_text_custom, this, true);
    View view = (View) inflater.inflate(R.layout.main, null);
    editText = (EditText) view.findViewById(R.id.id_number_custom);
    loadButton = (ImageButton) view.findViewById(R.id.load_data_button);
    loadButton.setVisibility(RelativeLayout.INVISIBLE);
    loadData();
} 

【讨论】:

  • 我试过这个 Usman 但它不起作用,视图返回 null。
  • 当LayoutInflater.inflate返回View时,不需要强制转换为View。
【解决方案2】:

你可以试试这样的:

customview 构造函数内部:

mContext = context;

接下来可以在customview中调用:

((MainActivity) mContext).updateText( text );

MainAcivity 内定义:

public void updateText(final String text) {

     TextView txtView = (TextView) findViewById(R.id.text);
     txtView.setText(text);
}

它对我有用。

【讨论】:

  • 多次调用findViewById() 不好且效率低下。如果您要重复使用它,请将其引用存储在 final 字段中。
【解决方案3】:

在你的构造函数中试试这个

MainActivity maniActivity = (MainActivity)context;
EditText firstName = (EditText) maniActivity.findViewById(R.id.display_name);

【讨论】:

    【解决方案4】:
    View Custmv;
    
     private void initViews() {
            inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            Custmv = inflater.inflate(R.layout.id_number_edit_text_custom, this, true);
            editText = (EditText) findViewById(R.id.id_number_custom);
            loadButton = (ImageButton) findViewById(R.id.load_data_button);
            loadButton.setVisibility(RelativeLayout.INVISIBLE);
            loadData();
        }
    
    private void loadData(){
            loadButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText firstName = (EditText) Custmv.getParent().findViewById(R.id.display_name);
                    firstName.setText("Some Text");
                }
            });
        }
    

    像这样试试。

    【讨论】:

    • 你没有得到它@Siddesh, editText = (EditText) findViewById(R.id.id_number_custom); loadButton = (ImageButton) findViewById(R.id.load_data_button);这很好用,我可以从 CustomView 布局中获取视图,我想从 MainLayout 中获取视图。
    • 好的,但为什么要处理自定义视图类的主布局?
    • 因为我想在与自定义视图交互后更新这些视图。
    • 我不确定,但在 onclick 监听器中尝试这样的 customview.this.getParent().findViewByID()
    • 它不起作用,因为我的 customView 的父级将为空,只要我只膨胀 customView 代表的布局。
    【解决方案5】:
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ImageHolder holder = null;
        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ImageHolder();
            editText = (EditText) row.findViewById(R.id.id_number_custom);
            loadButton = (ImageButton) row.findViewById(R.id.load_data_button);
            row.setTag(holder);
        } else {
            holder = (ImageHolder) row.getTag();
        }
    
    
        holder.editText.setText("Your Value");
        holder.loadButton.setImageBitmap("Your Bitmap Value");
        return row;
    }
    

    【讨论】:

    • 我想你在这里回答了一些其他问题。 . .
    • 如果你得到像空指针这样的错误,而不是根据你的代码在 edittext 和 Imagebutton 的初始化中出错......
    【解决方案6】:

    如果是固定布局,你可以这样做:

    public void onClick(View v) {
       ViewGroup parent = (ViewGroup) IdNumber.this.getParent();
       EditText firstName = (EditText) parent.findViewById(R.id.display_name);
       firstName.setText("Some Text");
    }
    

    如果你想在灵活的布局中找到 EditText,我稍后会帮助你。希望对您有所帮助。

    【讨论】:

    • parent 为空,只要我在扩展自定义视图所代表的布局。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多