Android Eclipse editText 在本地实例上 setText 后不显示
我通过“instanceOf”调用一些 EditText,因为我不一定知道它们的确切名称/ID。当我这样做来获取文本时,效果很好。但是当我尝试基于本地实例的 EditText setText() 时,它不会显示我传递给它的字符串。奇怪的是,当我 setText 然后记录 getText 时,它会正确记录它 - 它只是不会物理显示它。
我还测试了将可见性设置为 View.INVISIBLE 或 View.GONE 只是为了看看我是否可以与它交互,但它没有执行任何操作。
下面是设置文本然后记录它的代码片段:
LinearLayout mLayout = (LinearLayout) main_fields_holder.getChildAt(0);
LinearLayout mLayout2 = (LinearLayout) mLayout.getChildAt(0);
LinearLayout mLayout3 = (LinearLayout) mLayout2.getChildAt(1);
for(int i = 0; i < mLayout3.getChildCount(); i++){
View v = (View) mLayout3.getChildAt(i);
if (v instanceof EditText){
EditText e = (EditText) v;
e.setText("HELLO");
Log.v("USER TEXT", "Text is: " + e.getText().toString() + "!");
;
}
}
我还应该提到,我对按钮文本执行相同的 instanceOf 方法,并且它也不会显示我的 setText。
是否有某种原因导致无法操作 editText(或按钮)的本地实例?
顺便说一下,行 Log.v("USER TEXT", "Text is: " + e.getText().toString() + "!");确实给了我“你好!”。我什至不确定这对我的问题意味着什么。
主要布局文件是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
xmlns:android1="http://schemas.android.com/apk/res/android">
<ScrollView android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android1:focusable="false">
<LinearLayout android:id="@+id/item_edit_linear"
android:layout_height="wrap_content"
android:background="@drawable/backrepeat"
android:layout_width="fill_parent"
>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/add_item_text" android:text="Add New Item" android:textStyle="bold" android:paddingLeft="15dip" android1:paddingTop="8dip" android1:textSize="12pt" android1:textColor="@color/blue"></TextView>
</LinearLayout>
<LinearLayout android1:layout_height="wrap_content" android1:layout_width="fill_parent" android1:orientation="vertical" android1:paddingLeft="15dip" android1:background="@drawable/borderlines" android1:paddingBottom="15dip" android1:paddingTop="10dip" android1:id="@+id/spinner_holder_layout">
<TextView android1:layout_height="wrap_content" android1:layout_width="wrap_content" android1:paddingTop="5dip" android1:text="Item Name:" android1:textColor="@color/gray" android1:paddingBottom="5dip"></TextView>
<EditText android1:layout_height="wrap_content" android1:layout_width="290dip" android1:id="@+id/edit_item_title">
<requestFocus></requestFocus>
</EditText>
<TextView android1:layout_height="wrap_content" android1:layout_width="wrap_content" android1:paddingTop="5dip" android1:id="@+id/textView9" android1:text="Select Category:" android1:textColor="@color/gray" android1:paddingBottom="5dip"></TextView>
<Spinner android1:layout_height="wrap_content" android1:layout_width="wrap_content" android1:prompt="@string/edit_item_category_spinner_text" android1:id="@+id/edit_item_category_spinner" android1:entries="@array/categoryList" android1:paddingBottom="10dip"></Spinner>
<LinearLayout android1:layout_height="wrap_content" android1:layout_width="fill_parent" android1:orientation="vertical" android1:id="@+id/item_spinner_layout" android1:visibility="gone">
<TextView android1:layout_height="wrap_content" android1:layout_width="wrap_content" android1:paddingTop="5dip" android1:id="@+id/textView10" android1:text="Select Item" android1:textColor="@color/gray" android1:paddingBottom="5dip"></TextView>
<Spinner android1:layout_height="wrap_content" android1:layout_width="wrap_content" android1:prompt="@string/edit_item_item_spinner_text" android1:paddingBottom="10dip" android1:id="@+id/edit_item_spinner"></Spinner>
</LinearLayout>
</LinearLayout>
<LinearLayout android1:layout_height="wrap_content" android1:layout_width="fill_parent" android1:orientation="vertical" android1:id="@+id/editItemFields">
</LinearLayout>
<LinearLayout android1:layout_marginLeft="15dip" android1:layout_width="fill_parent" android1:layout_height="wrap_content" android1:id="@+id/date_time_container" android1:visibility="gone">
<LinearLayout android1:layout_width="wrap_content" android1:layout_height="fill_parent" android1:id="@+id/linearLayout7" android1:orientation="vertical">
<TextView android1:layout_width="fill_parent" android1:id="@+id/dep_date_text" android1:textColor="@color/gray" android1:layout_marginBottom="10dip" android1:text="Departure Date:" android1:gravity="right|center" android1:layout_height="36dip"></TextView>
<TextView android1:layout_width="fill_parent" android1:id="@+id/dep_time_text" android1:textColor="@color/gray" android1:layout_marginBottom="9dip" android1:text="Depature Time:" android1:gravity="right|center" android1:layout_height="36dip"></TextView>
<TextView android1:layout_width="fill_parent" android1:id="@+id/arr_date_text" android1:textColor="@color/gray" android1:layout_marginBottom="9dip" android1:text="Arrival Date:" android1:gravity="right|center" android1:layout_height="36dip"></TextView>
<TextView android1:layout_width="fill_parent" android1:id="@+id/arr_time_text" android1:textColor="@color/gray" android1:layout_marginBottom="9dip" android1:text="Arrival Time:" android1:gravity="right|center" android1:layout_height="36dip"></TextView>
</LinearLayout>
<LinearLayout android1:layout_marginLeft="5dip" android1:layout_width="wrap_content" android1:layout_height="fill_parent" android1:id="@+id/linearLayout8" android1:orientation="vertical">
<Button android1:layout_width="wrap_content" android1:layout_height="45dip" android1:id="@+id/departure_date" android1:minWidth="125dip" android1:text="Button"></Button>
<Button android1:layout_width="wrap_content" android1:layout_height="45dip" android1:id="@+id/departure_time" android1:minWidth="125dip" android1:text="Button"></Button>
<Button android1:layout_width="wrap_content" android1:layout_height="45dip" android1:id="@+id/arrival_date" android1:minWidth="125dip" android1:text="Button"></Button>
<Button android1:layout_width="wrap_content" android1:layout_height="45dip" android1:id="@+id/arrival_time" android1:minWidth="125dip" android1:text="Button"></Button>
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:id="@+id/editItemLinearLayout"
android:layout_height="fill_parent" android:orientation="vertical" android1:paddingLeft="15dip" android1:visibility="gone">
<TextView android:text="Cost" android:id="@+id/textView1"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/edit_item_cost">
</EditText>
<TextView android:text="Contact Phone" android:id="@+id/textView2"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/edit_item_phone">
</EditText>
<TextView android:text="Carrier" android:id="@+id/textView3"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/edit_item_carrier">
</EditText>
<TextView android:text="Names" android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/edit_item_names">
</EditText>
<TextView android:text="Departure Location" android:id="@+id/textView5"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/edit_item_departure_location">
</EditText>
<TextView android:text="Arrival Location" android:id="@+id/textView6"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/edit_item_arrival_location">
</EditText>
<TextView android:id="@+id/textView7" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Date"></TextView>
<Button android:text="Button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/edit_item_reminder_date"></Button>
<TextView android:id="@+id/textView8" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Time"></TextView>
<Button android:text="Button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/edit_item_reminder_time"></Button>
</LinearLayout>
<RelativeLayout android1:layout_width="fill_parent" android1:layout_marginTop="50dip"
android1:id="@+id/relativeLayout1" android1:layout_height="fill_parent">
<Button android1:id="@+id/edit_item_confirm" android1:layout_width="wrap_content" android1:layout_height="wrap_content" android1:background="@drawable/savebutton" android1:layout_alignParentBottom="true"></Button>
<Button android1:width="159dip" android1:background="@drawable/cancelbutton" android1:layout_width="wrap_content" android1:id="@+id/item_creation_cancel" android1:layout_height="wrap_content" android1:layout_alignParentRight="true" android1:layout_alignParentBottom="true"></Button>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
通过代码,我使用以下内容膨胀 editItemFields 布局:
<LinearLayout android1:paddingLeft="15dip" android1:layout_width="fill_parent"
android1:background="@color/white" android1:id="@+id/flight_edit_container"
android1:orientation="vertical" android1:layout_height="fill_parent"
xmlns:android1="http://schemas.android.com/apk/res/android" android1:paddingTop="10dip">
<LinearLayout android1:layout_height="wrap_content" android1:layout_width="fill_parent" android1:id="@+id/linearLayout6">
<LinearLayout android1:layout_height="fill_parent" android1:layout_width="wrap_content" android1:id="@+id/linearLayout7" android1:orientation="vertical">
<TextView android1:id="@+id/TextView02" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="10dip" android1:text="Airline:"></TextView>
<TextView android1:text="Flight #:" android1:id="@+id/TextView05" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="10dip"></TextView>
<TextView android1:text="Gate:" android1:id="@+id/TextView04" android1:textColor="@color/gray" android1:gravity="right|center" android1:layout_width="100dip" android1:layout_height="36dip" android1:layout_marginBottom="10dip"></TextView>
<TextView android1:id="@+id/TextView09" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:text="Departure Location:" android1:layout_height="36dip" android1:layout_marginBottom="10dip"></TextView>
<TextView android1:text="Departure Airport:" android1:id="@+id/TextView14" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="10dip"></TextView>
<TextView android1:text="Confirmation #:" android1:id="@+id/TextView12" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
<TextView android1:gravity="right|center" android1:text="Destination:" android1:id="@+id/TextView08" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
<TextView android1:text="Arrival Airport:" android1:id="@+id/TextView15" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
<TextView android1:text="Cost:" android1:id="@+id/TextView09" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
<TextView android1:text="Class:" android1:id="@+id/TextView08" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
<TextView android1:text="Seat:" android1:id="@+id/TextView11" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
<TextView android1:text="Travelers:" android1:id="@+id/TextView12" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
<TextView android1:text="Memo:" android1:id="@+id/TextView13" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
</LinearLayout>
<LinearLayout android1:layout_height="fill_parent" android1:layout_width="wrap_content" android1:id="@+id/linearLayout8" android1:orientation="vertical" android1:layout_marginLeft="5dip">
<EditText android1:layout_width="190dip" android1:layout_height="45dip" android1:id="@+id/flight_airline"></EditText>
<EditText android1:layout_width="190dip" android1:id="@+id/flight_number" android1:layout_height="45dip"></EditText>
<EditText android1:layout_width="190dip" android1:id="@+id/flight_gate" android1:layout_height="45dip"></EditText>
<EditText android1:layout_width="190dip" android1:id="@+id/flight_deplocation" android1:layout_height="45dip"></EditText>
<EditText android1:layout_width="190dip" android1:id="@+id/flight_depairport" android1:layout_height="45dip"></EditText>
<EditText android1:layout_width="190dip" android1:id="@+id/flight_confirmation" android1:layout_height="45dip"></EditText>
<EditText android1:layout_height="45dip" android1:layout_width="190dip" android1:id="@+id/flight_destination"></EditText>
<EditText android1:layout_height="45dip" android1:layout_width="190dip" android1:id="@+id/flight_arrairport">
</EditText>
<EditText android1:layout_width="190dip" android1:layout_height="45dip" android1:id="@+id/flight_cost"></EditText>
<EditText android1:layout_width="190dip" android1:layout_height="45dip" android1:id="@+id/flight_class"></EditText>
<EditText android1:layout_width="190dip" android1:layout_height="45dip" android1:id="@+id/flight_seat"></EditText>
<Button android1:text="Button" android1:minWidth="125dip" android1:id="@+id/flight_travelers" android1:layout_width="wrap_content" android1:layout_height="45dip"></Button>
<EditText android1:layout_width="190dip" android1:id="@+id/flight_memo" android1:layout_height="45dip"></EditText>
</LinearLayout>
</LinearLayout>
</LinearLayout>
我基本上向下钻取布局,直到到达保存 editText 的线性布局。
最后,这里是膨胀布局的代码片段:
main_fields_holder = (LinearLayout) findViewById(R.id.editItemFields);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.flight_fields,
null);
main_fields_holder.addView(view);
更新: 只是为了测试,我添加了这一行,看看我是否可以通过 id: 硬引用它:
EditText a = ((EditText)findViewById(R.id.flight_airline));
a.setText("HELLO");
但它仍然不会显示。我膨胀容纳这些项目的布局是否与此有关?基本上,当选择微调项目时,布局会膨胀,我尝试在该布局中设置项目的文本,但无济于事。顺便说一句,我仍然可以在设置 EditText 后追踪它的值,但bugger不会实际出现。
I call on a few EditTexts via "instanceOf" because I don't necessarily know the exact names / ids of them. When I do this to get the text, it works just fine. But when I try to setText() based on the local instanceOf EditText, it won't display the string I am passing it. Oddly enough, when I setText and then Log the getText, it logs it correctly -- it just won't physically display it.
I also tested setting the visibility to View.INVISIBLE or View.GONE just to see if i can interact with it at all, and it doesn't do anything.
Here is the code snippet that sets the text then logs it:
LinearLayout mLayout = (LinearLayout) main_fields_holder.getChildAt(0);
LinearLayout mLayout2 = (LinearLayout) mLayout.getChildAt(0);
LinearLayout mLayout3 = (LinearLayout) mLayout2.getChildAt(1);
for(int i = 0; i < mLayout3.getChildCount(); i++){
View v = (View) mLayout3.getChildAt(i);
if (v instanceof EditText){
EditText e = (EditText) v;
e.setText("HELLO");
Log.v("USER TEXT", "Text is: " + e.getText().toString() + "!");
;
}
}
I should also mention that I do the same instanceOf method with button text and it won't display my setText either.
Is there some reason that a local instance of an editText (or button) cannot be manipulated?
By the way, the line Log.v("USER TEXT", "Text is: " + e.getText().toString() + "!"); DOES give me "HELLO!". I'm not even sure what that means for my problem.
The main layout file is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
xmlns:android1="http://schemas.android.com/apk/res/android">
<ScrollView android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android1:focusable="false">
<LinearLayout android:id="@+id/item_edit_linear"
android:layout_height="wrap_content"
android:background="@drawable/backrepeat"
android:layout_width="fill_parent"
>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/add_item_text" android:text="Add New Item" android:textStyle="bold" android:paddingLeft="15dip" android1:paddingTop="8dip" android1:textSize="12pt" android1:textColor="@color/blue"></TextView>
</LinearLayout>
<LinearLayout android1:layout_height="wrap_content" android1:layout_width="fill_parent" android1:orientation="vertical" android1:paddingLeft="15dip" android1:background="@drawable/borderlines" android1:paddingBottom="15dip" android1:paddingTop="10dip" android1:id="@+id/spinner_holder_layout">
<TextView android1:layout_height="wrap_content" android1:layout_width="wrap_content" android1:paddingTop="5dip" android1:text="Item Name:" android1:textColor="@color/gray" android1:paddingBottom="5dip"></TextView>
<EditText android1:layout_height="wrap_content" android1:layout_width="290dip" android1:id="@+id/edit_item_title">
<requestFocus></requestFocus>
</EditText>
<TextView android1:layout_height="wrap_content" android1:layout_width="wrap_content" android1:paddingTop="5dip" android1:id="@+id/textView9" android1:text="Select Category:" android1:textColor="@color/gray" android1:paddingBottom="5dip"></TextView>
<Spinner android1:layout_height="wrap_content" android1:layout_width="wrap_content" android1:prompt="@string/edit_item_category_spinner_text" android1:id="@+id/edit_item_category_spinner" android1:entries="@array/categoryList" android1:paddingBottom="10dip"></Spinner>
<LinearLayout android1:layout_height="wrap_content" android1:layout_width="fill_parent" android1:orientation="vertical" android1:id="@+id/item_spinner_layout" android1:visibility="gone">
<TextView android1:layout_height="wrap_content" android1:layout_width="wrap_content" android1:paddingTop="5dip" android1:id="@+id/textView10" android1:text="Select Item" android1:textColor="@color/gray" android1:paddingBottom="5dip"></TextView>
<Spinner android1:layout_height="wrap_content" android1:layout_width="wrap_content" android1:prompt="@string/edit_item_item_spinner_text" android1:paddingBottom="10dip" android1:id="@+id/edit_item_spinner"></Spinner>
</LinearLayout>
</LinearLayout>
<LinearLayout android1:layout_height="wrap_content" android1:layout_width="fill_parent" android1:orientation="vertical" android1:id="@+id/editItemFields">
</LinearLayout>
<LinearLayout android1:layout_marginLeft="15dip" android1:layout_width="fill_parent" android1:layout_height="wrap_content" android1:id="@+id/date_time_container" android1:visibility="gone">
<LinearLayout android1:layout_width="wrap_content" android1:layout_height="fill_parent" android1:id="@+id/linearLayout7" android1:orientation="vertical">
<TextView android1:layout_width="fill_parent" android1:id="@+id/dep_date_text" android1:textColor="@color/gray" android1:layout_marginBottom="10dip" android1:text="Departure Date:" android1:gravity="right|center" android1:layout_height="36dip"></TextView>
<TextView android1:layout_width="fill_parent" android1:id="@+id/dep_time_text" android1:textColor="@color/gray" android1:layout_marginBottom="9dip" android1:text="Depature Time:" android1:gravity="right|center" android1:layout_height="36dip"></TextView>
<TextView android1:layout_width="fill_parent" android1:id="@+id/arr_date_text" android1:textColor="@color/gray" android1:layout_marginBottom="9dip" android1:text="Arrival Date:" android1:gravity="right|center" android1:layout_height="36dip"></TextView>
<TextView android1:layout_width="fill_parent" android1:id="@+id/arr_time_text" android1:textColor="@color/gray" android1:layout_marginBottom="9dip" android1:text="Arrival Time:" android1:gravity="right|center" android1:layout_height="36dip"></TextView>
</LinearLayout>
<LinearLayout android1:layout_marginLeft="5dip" android1:layout_width="wrap_content" android1:layout_height="fill_parent" android1:id="@+id/linearLayout8" android1:orientation="vertical">
<Button android1:layout_width="wrap_content" android1:layout_height="45dip" android1:id="@+id/departure_date" android1:minWidth="125dip" android1:text="Button"></Button>
<Button android1:layout_width="wrap_content" android1:layout_height="45dip" android1:id="@+id/departure_time" android1:minWidth="125dip" android1:text="Button"></Button>
<Button android1:layout_width="wrap_content" android1:layout_height="45dip" android1:id="@+id/arrival_date" android1:minWidth="125dip" android1:text="Button"></Button>
<Button android1:layout_width="wrap_content" android1:layout_height="45dip" android1:id="@+id/arrival_time" android1:minWidth="125dip" android1:text="Button"></Button>
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:id="@+id/editItemLinearLayout"
android:layout_height="fill_parent" android:orientation="vertical" android1:paddingLeft="15dip" android1:visibility="gone">
<TextView android:text="Cost" android:id="@+id/textView1"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/edit_item_cost">
</EditText>
<TextView android:text="Contact Phone" android:id="@+id/textView2"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/edit_item_phone">
</EditText>
<TextView android:text="Carrier" android:id="@+id/textView3"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/edit_item_carrier">
</EditText>
<TextView android:text="Names" android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/edit_item_names">
</EditText>
<TextView android:text="Departure Location" android:id="@+id/textView5"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/edit_item_departure_location">
</EditText>
<TextView android:text="Arrival Location" android:id="@+id/textView6"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</TextView>
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/edit_item_arrival_location">
</EditText>
<TextView android:id="@+id/textView7" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Date"></TextView>
<Button android:text="Button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/edit_item_reminder_date"></Button>
<TextView android:id="@+id/textView8" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Time"></TextView>
<Button android:text="Button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/edit_item_reminder_time"></Button>
</LinearLayout>
<RelativeLayout android1:layout_width="fill_parent" android1:layout_marginTop="50dip"
android1:id="@+id/relativeLayout1" android1:layout_height="fill_parent">
<Button android1:id="@+id/edit_item_confirm" android1:layout_width="wrap_content" android1:layout_height="wrap_content" android1:background="@drawable/savebutton" android1:layout_alignParentBottom="true"></Button>
<Button android1:width="159dip" android1:background="@drawable/cancelbutton" android1:layout_width="wrap_content" android1:id="@+id/item_creation_cancel" android1:layout_height="wrap_content" android1:layout_alignParentRight="true" android1:layout_alignParentBottom="true"></Button>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Through code, I inflate the editItemFields Layout with the following:
<LinearLayout android1:paddingLeft="15dip" android1:layout_width="fill_parent"
android1:background="@color/white" android1:id="@+id/flight_edit_container"
android1:orientation="vertical" android1:layout_height="fill_parent"
xmlns:android1="http://schemas.android.com/apk/res/android" android1:paddingTop="10dip">
<LinearLayout android1:layout_height="wrap_content" android1:layout_width="fill_parent" android1:id="@+id/linearLayout6">
<LinearLayout android1:layout_height="fill_parent" android1:layout_width="wrap_content" android1:id="@+id/linearLayout7" android1:orientation="vertical">
<TextView android1:id="@+id/TextView02" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="10dip" android1:text="Airline:"></TextView>
<TextView android1:text="Flight #:" android1:id="@+id/TextView05" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="10dip"></TextView>
<TextView android1:text="Gate:" android1:id="@+id/TextView04" android1:textColor="@color/gray" android1:gravity="right|center" android1:layout_width="100dip" android1:layout_height="36dip" android1:layout_marginBottom="10dip"></TextView>
<TextView android1:id="@+id/TextView09" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:text="Departure Location:" android1:layout_height="36dip" android1:layout_marginBottom="10dip"></TextView>
<TextView android1:text="Departure Airport:" android1:id="@+id/TextView14" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="10dip"></TextView>
<TextView android1:text="Confirmation #:" android1:id="@+id/TextView12" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
<TextView android1:gravity="right|center" android1:text="Destination:" android1:id="@+id/TextView08" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
<TextView android1:text="Arrival Airport:" android1:id="@+id/TextView15" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
<TextView android1:text="Cost:" android1:id="@+id/TextView09" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
<TextView android1:text="Class:" android1:id="@+id/TextView08" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
<TextView android1:text="Seat:" android1:id="@+id/TextView11" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
<TextView android1:text="Travelers:" android1:id="@+id/TextView12" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
<TextView android1:text="Memo:" android1:id="@+id/TextView13" android1:textColor="@color/gray" android1:layout_width="fill_parent" android1:gravity="right|center" android1:layout_height="36dip" android1:layout_marginBottom="9dip"></TextView>
</LinearLayout>
<LinearLayout android1:layout_height="fill_parent" android1:layout_width="wrap_content" android1:id="@+id/linearLayout8" android1:orientation="vertical" android1:layout_marginLeft="5dip">
<EditText android1:layout_width="190dip" android1:layout_height="45dip" android1:id="@+id/flight_airline"></EditText>
<EditText android1:layout_width="190dip" android1:id="@+id/flight_number" android1:layout_height="45dip"></EditText>
<EditText android1:layout_width="190dip" android1:id="@+id/flight_gate" android1:layout_height="45dip"></EditText>
<EditText android1:layout_width="190dip" android1:id="@+id/flight_deplocation" android1:layout_height="45dip"></EditText>
<EditText android1:layout_width="190dip" android1:id="@+id/flight_depairport" android1:layout_height="45dip"></EditText>
<EditText android1:layout_width="190dip" android1:id="@+id/flight_confirmation" android1:layout_height="45dip"></EditText>
<EditText android1:layout_height="45dip" android1:layout_width="190dip" android1:id="@+id/flight_destination"></EditText>
<EditText android1:layout_height="45dip" android1:layout_width="190dip" android1:id="@+id/flight_arrairport">
</EditText>
<EditText android1:layout_width="190dip" android1:layout_height="45dip" android1:id="@+id/flight_cost"></EditText>
<EditText android1:layout_width="190dip" android1:layout_height="45dip" android1:id="@+id/flight_class"></EditText>
<EditText android1:layout_width="190dip" android1:layout_height="45dip" android1:id="@+id/flight_seat"></EditText>
<Button android1:text="Button" android1:minWidth="125dip" android1:id="@+id/flight_travelers" android1:layout_width="wrap_content" android1:layout_height="45dip"></Button>
<EditText android1:layout_width="190dip" android1:id="@+id/flight_memo" android1:layout_height="45dip"></EditText>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I basically drill down into the layouts until i am at the linear layout that holds my editTexts.
Finally, here is the code snippets that inflate the layout:
main_fields_holder = (LinearLayout) findViewById(R.id.editItemFields);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.flight_fields,
null);
main_fields_holder.addView(view);
UPDATE:
Just to test, I added this line to see if i can hard reference it by id:
EditText a = ((EditText)findViewById(R.id.flight_airline));
a.setText("HELLO");
And it STILL won't show up. Could the fact that I inflate the layout that holds these items have anything to do with it? Basically when a spinner item is selected, a layout is inflated and I try to set the Text of the items in that layout, but to no avail. Btw, I can also still trace out the value of the EditText after setting it, but the bugger just won't physically show up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在创建 EditText 时设置它的 ID,此时您应该能够使用它。
e.setId(15); //15 是任何正整数
相反,你可以随时尝试
e.getEditableText();
Set the ID of the EditText when you create it when you should be able to work with it.
e.setId(15); //15 is any positive integer
On the opposite you can always try
e.getEditableText();
尝试
try
仅供参考,我想通了。无论出于何种原因,应用程序需要一些时间来完成所有获取信息并设置它们的函数,因此我添加了一个后处理程序:
这1/2秒的等待让信息得以正确显示。感谢大家的回答
FYI I figured this out. For whatever reason, the app needed a moment to go through all of the functions that grab the information and set them, so I added a post handler:
This 1/2 second wait allowed the information to be displayed properly. Thanks for everyone's answers
使用
findViewById()
检索Views
时,似乎存在非常微妙但重要的差异。如果您“本地”调用
findViewById(R.id.yourEditText)
,例如在onResume()
方法的顶级行中,则EditText 似乎表现正确,
setText(...)
在显示屏中显示新值。但是如果你将 Activity 放入另一个对象(例如某些 POJO)的成员变量中,并且你这样做:
你仍然会找到正确的难以区分的 EditText-Object,但是
setText()
不会不再工作了。我不知道为什么。我花了很多时间才找到这个(API 级别 7-项目)。呵呵!!!
(哎呀 - 忘了吧。我没有 POJO,但是一些错误的地图缓存给了我一个旧的活动对象)
There seem to be very subtle but important differences when retrieving
Views
withfindViewById()
.If you call
findViewById(R.id.yourEditText)
"natively" e.g. in a top-level-line of theonResume()
Method, theEditText
seems to behave correctly,setText(...)
shows the new Value in the display.But if you put the Activity into a member variable of another Object (e.g. some POJO), and you do this:
you still will find the right an indistinguishable EditText-Object, but
setText()
doesn't work anymore. I don't know why.I spent a lot of hours to find this (API Level 7-Project). Huuuh!!!
(Oooops - forget it. I didn't have a POJO, but some wrong Map-caching which gave me an old Activity-Object)
听起来很像您没有得到您认为的
EditText
。如果您提供更多详细信息,也许我们可以帮助您找到合适的。更新:
看来您确实确实知道ID,问题是它们不是唯一的。解决方案是从视图层次结构中的已知点开始,您知道它们是唯一的。例如,在扩充
editItemFields
布局后,您可以在view
变量中引用该视图层次结构的根。然后您可以执行以下操作:您将获得该特定项目的
flight_number
EditText
。It sounds very much like you're not getting the
EditText
that you think you are. If you give some more detail, maybe we can help you find the right one.Update:
So it seems that you do actually know the IDs, the problem is that they're not unique. The solution is to start from a known point in the view hierarchy where you know they're unique. For example, after you inflate your
editItemFields
layout, you have a reference to the root of that view hierarchy in yourview
variable. You can then do something like:And you'll get the
flight_number
EditText
for that particular item.尝试这样做:
这是一个比当前标记为已接受的解决方案更好的解决方案。
当它实际准备好时,这会将其发布到 UI,而不是等待 1/2 秒。
Try doing:
Which is a much better solution than the one currently marked as accepted.
This will post it to the UI when it's actually ready, instead waiting around for 1/2 second.