Tôi mới phát triển Android và tôi đang cố viết một ứng dụng nhỏ, đơn giản. Ứng dụng này không nên làm gì ngoài việc đọc văn bản từ 2 EditTexts và khi người dùng nhấn nút "gửi", nó sẽ viết văn bản thành 2 TextViews. Mỗi TextView trong một Fragment.Gọi hàm findViewById trong Fragment để tìm một nút trả về null
Nhưng tôi không thể thêm onClickEvent vào nút, vì findViewById (R.id.sendTextButton) luôn trả về giá trị rỗng. Tôi đã cố gắng để ứng dụng này hoạt động nhưng hiện tại tôi không thể tìm thấy bất kỳ giải pháp nào.
Sau đây là các CodeBlocks liên quan:
Các onCreateView chức năng của đoạn, mà nên xử lý đầu vào đầu tiên (HeadlineFragment.java):
Button sendButton = null;
View inflatedView = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.inflatedView = inflater.inflate(R.layout.headline_view, container, false);
sendButton = (Button) inflatedView.findViewById(R.id.sendTextButton);
if(sendButton == null)
{
Log.d("debugCheck", "HeadFrag: sendButton is null");
return inflatedView;
}
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String headline = ((EditText) v.findViewById(R.id.enterHeadlineText)).getText().toString();
((TextView) v.findViewById(R.layout.headline_view)).setText(headline);
}
});
return inflatedView;
}
Các tập tin xml với cách bố trí (activity_main.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/enterHeadlineText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/enterHeadlineTextHint"
android:maxLines="1"
android:inputType="textCapSentences"
/>
<EditText
android:id="@+id/enterMessageText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="@string/enterMessageTextHint"
android:maxLines="1"
android:inputType="textCapSentences"
/>
<Button
android:id="@+id/sendTextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/sendTextButton"
/>
<fragment android:name="com.example.mysecondapplication.HeadlineFragment"
android:id="@+id/headlineFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
/>
<fragment android:name="com.example.mysecondapplication.MessageFragment"
android:id="@+id/messageFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
/>
</LinearLayout>
Và file xml với TextView, mà nên chứa văn bản (headline_view.xml):
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/headline_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:text="@string/hello_world"
/>
Có cách nào để xem activity_main.xml từ bên trong Phân đoạn của tôi hoặc tôi có phải truy cập nút từ một tệp khác không? –
bạn muốn đạt được điều gì. Tại sao bạn cần một nút được khai báo bên trong bố cục hoạt động bên trong đoạn? Nếu nó thuộc về khung nhìn được trả về bởi đoạn bạn nên khai báo bên trong headline_view.xm – Blackbelt
Tôi muốn thiết lập các sự kiện nút, thay đổi nội dung của các mảnh bên trong các mảnh vì tôi nghĩ đây sẽ là một kiểu tốt. Nhưng bây giờ tôi sẽ cố gắng làm điều đó theo một cách khác, cảm ơn! –