我创建了一个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");
当我运行上面的代码时,没有任何反应:文本没有从&#34; OK&#34;到&#34; a&#34;如预期的那样。
答案 0 :(得分:0)
<强>更新:强>
更新您Utility.java
课程,如下所示:
public class Utility {
public static View getViewOfLayout(View layout, int viewId) {
View view = layout.findViewById(viewId);
return view;
}
}
在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();