2013-08-29 40 views
16

Tôi đã viết Chế độ xem tùy chỉnh và tôi muốn cập nhật một số chế độ xem khác sau khi tương tác với chế độ xem tùy chỉnh của mình.Android findViewById() trong Chế độ xem tùy chỉnh

chính Layout:

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

Custom View Layout:

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

Custom View Class, nhà xây dựng và người nghe:

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

Vấn đề là EditText firstName = (EditText) findViewById(R.id.display_name); trả về giá trị rỗng. Tôi biết rằng chỉ cần gọi findViewById() sẽ tìm kiếm chế độ xem trong bố cục mà tôi đã thổi phồng nó.

Nếu có thể, làm thế nào tôi có thể xem được EditText với id: display_name từ bố cục chính?

Cảm ơn trước.

+0

không, tôi không muốn nhận chế độ xem tùy chỉnh của mình, tôi muốn có chế độ xem khác từ mainLayout, với id diplay_name. – Jilberta

+0

bạn đang tăng cấp xml nhưng không lưu trữ chế độ xem được trả lại. bạn có thể tìm thấy bằng id trên chế độ xem đó. – Siddhesh

Trả lời

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

hãy thử như thế này.

+0

Bạn không nhận được @Siddesh, editText = (EditText) findViewById (R.id.id_number_custom); loadButton = (ImageButton) findViewById (R.id.load_data_button); điều này hoạt động tốt, tôi có thể có quan điểm từ bố trí CustomView, tôi muốn nhận được Views từ MainLayout. – Jilberta

+0

ok nhưng tại sao bạn muốn xử lý bố cục chính từ lớp xem tùy chỉnh? – Siddhesh

+0

vì tôi muốn cập nhật các chế độ xem đó sau khi tương tác với chế độ xem tùy chỉnh của tôi. – Jilberta

1

Thay đổi phương pháp của bạn như sau và kiểm tra xem nó sẽ làm việc

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

Tôi đã thử Usman này nhưng nó không hoạt động, view đang trả về null. – Jilberta

+0

Không cần phải truyền tới Chế độ xem khi LayoutInflater.inflate trả về Chế độ xem. – vilpe89

0

Nếu đó là bố trí cố định bạn có thể làm như thế:

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

Nếu bạn muốn tìm EditText trong cách bố trí linh hoạt, tôi sẽ giúp bạn sau. Hy vọng điều này giúp đỡ.

+0

cấp độ gốc là không, miễn là tôi đang tăng bố cục mà chế độ xem tùy chỉnh thể hiện. – Jilberta

0
@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; 
} 
+1

Tôi nghĩ bạn đang trả lời một số câu hỏi khác ở đây. . . – Jilberta

+0

Nếu bạn nhận được lỗi như con trỏ null hơn bạn mắc lỗi khi khởi tạo edittext và Imagebutton theo mã của bạn .... –

+0

không có vấn đề nào không phải trong editText hay trong imageButton – Jilberta

0

Hãy thử điều này trong constructor của bạn

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

Bạn có thể thử một cái gì đó như thế này:

Bên customview constructor:

mContext = context; 

Tiếp bên customview bạn có thể gọi:

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

Bên MainAcivity xác định:

public void updateText(final String text) { 

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

Nó làm việc cho tôi.

+0

gọi 'findViewById()' nhiều lần là xấu và không hiệu quả. Nếu bạn định sử dụng nó nhiều lần, hãy lưu trữ một tham chiếu đến nó trong trường 'final'. –

Các vấn đề liên quan