2017-03-27 73 views
-1

我有我創建了一個XML配置文件:如何訪問佈局視圖?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:id="@+id/editText_dateStart" 
     android:textColor="@color/colorPrimaryDark" 
     android:textColorHint="@color/colorPrimary" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:padding="20dp" 
     android:hint="Start Date" 
     android:inputType="date" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <EditText 
     android:id="@+id/editText_dateEnd" 
     android:textColor="@color/colorPrimaryDark" 
     android:textColorHint="@color/colorPrimary" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:padding="20dp" 
     android:hint="End Date" 
     android:inputType="date" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:id="@+id/button_ok" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:layout_marginBottom="15dp" 
     android:text="Ok" 
     android:textColor="#ffffffff" 
     android:background="@color/colorPrimaryDark" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

我要訪問的佈局,並在一個類中的方法,我寫返回佈局的觀點:

public class Utility { 
// 6 - LayoutInflate tool start 
    public static View getViewOfLayout(Activity activity, int layoutId, int viewId) { 
     LayoutInflater lin = activity.getLayoutInflater(); 
     View view = lin.inflate(layoutId, null); 
     view = view.findViewById(viewId); 
     return view; 
    } 
    // LayoutInflate tool end 
} 

我也希望能夠訪問Activity類的觀點:

Button button = (Button) Utility.getViewOfLayout(this, R.layout.datedialogbox, R.id.button_ok); 
      button.setText("a"); 

當我運行上面的代碼,什麼都沒有發生:文並沒有「一」預期從「OK」改變。

+2

當你說什麼都沒有發生,你是什麼意思?當您在代碼中設置斷點時,值是否爲空?如果是這樣的話,等等。 –

+0

它的按鈕的平均文本沒有設置爲「OK」,,,我設置的斷點按鈕值不爲空,,, –

+0

爲什麼你這樣做,對於每個視圖映射,你實際上整個佈局。 – YakuZa

回答

0

更新:

  1. 更新你Utility.java類,如下:

    public class Utility { 
    
        public static View getViewOfLayout(View layout, int viewId) { 
    
         View view = layout.findViewById(viewId); 
         return view; 
        } 
    } 
    
  2. 在你Activity類:

    .............. 
    .................... 
    LayoutInflater inflater = activity.getLayoutInflater(); 
    View dialogLayout = inflater.inflate(R.layout.datedialogbox, null); 
    
    Button button = (Button) Utility.getViewOfLayout(dialogLayout, R.id.button_ok); 
    
    button.setText("BUTTON"); 
    button.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View view) { 
          // Do something... 
         } 
        }); 
    
    
    ...................... 
    ............................. 
    

如果你的目的是爲了創造一個Custom Dialog那麼你可以用最少的代碼如下容易做到這一點:

  AlertDialog.Builder dialogBuilder; 
      AlertDialog dialog; 

      LayoutInflater inflater = activity.getLayoutInflater(); 
      View dialogLayout = inflater.inflate(R.layout.datedialogbox, null); 

      // Views 
      final EditText editTextDateStart = (EditText) dialogLayout.findViewById(R.id.editText_dateStart); 
      final EditText editTextDateEnd = (EditText) dialogLayout.findViewById(R.id.editText_dateEnd); 
      Button button = (Button) dialogLayout.findViewById(R.id.button_ok); 

      button.setText("BUTTON"); // Change button text 
      button.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 

        String dateStart = editTextDateStart.getText().toString(); 
        String dateEnd = editTextDateEnd.getText().toString(); 

        // Do something 
       } 
      }); 

      dialogBuilder = new AlertDialog.Builder(activity); 
      dialogBuilder.setView(dialogLayout); 

      dialog = dialogBuilder.create(); 
      dialog.show(); 
+0

不工作:( –

+0

@MuhammadSalmanRafique:試試我的 – FAT

+0

我可以但不工作 –

相關問題