[英]Custom Layout Interact with another layouts TextView
我有一個如下所示的自定義布局,我想從該布局文件中更新TextViews文本。 但是TextView根本不在布局文件中。
抱歉,我不知道如何正確描述我要實現的內容。 如果有人能就正確的術語提供建議,將不勝感激。
基本上,當我單擊從com.grogorian.android.control.MinutePicker放大的按鈕時,我想將TextView從AM更改為PM。 我在com.grogorian.android.control.MinutePicker中使用下面的java,但一直得到空指針
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/L"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal">
<com.grogorian.android.control.MinutePicker
android:id="@+id/Picker2"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.grogorian.android.control.MinutePicker>
<TextView android:id="@+id/AMPMIdentifier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AM" />
這是Java
LinearLayout L = (LinearLayout)findViewById(R.id.L);
TextView Identifier = (TextView)L.findViewById(R.id.AMPMIdentifier);
Identifier.setText("PM");
編輯:這是來自的代碼
but = new Button( context );
but.setTextSize( TEXT_SIZE );
but.setText( "-" );
// Decrement once for a click
but.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
LinearLayout L = (LinearLayout)findViewById(R.id.L);
TextView Identifier = (TextView)L.findViewById(R.id.AMPMIdentifier);
Identifier.setText("PM");
}
});
this.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ) );
LayoutParams elementParams = new LinearLayout.LayoutParams( ELEMENT_WIDTH, ELEMENT_HEIGHT );
addView( but, elementParams );
如果我猜到了,現在您正在嘗試在OnCLickListener
偵聽器中找到發布的布局文件中的LinearLayout
L (您可能將其用作Activity
的布局?!)。 這將失敗,因為將找不到LinearLayout
並且該對象將為null
。 如果這是您在做什么,請嘗試另一種方法:
but.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
LinearLayout parent = (LinearLayout) v.getParent(); // I assumed your MinutePicker extends LinearLayout
LinearLayout L = (LinearLayout) parent.getParent();
TextView Identifier = (TextView)L.findViewById(R.id.AMPMIdentifier);
Identifier.setText("PM");
}
});
我還沒有測試上面的代碼(嗯,我什至不知道您的MinutePicker
是如何構建的)。 如果這不是問題,則可能要添加可能獲得的完整異常堆棧跟蹤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.