【发布时间】:2014-03-06 23:08:48
【问题描述】:
我正在尝试设置此活动“ChoiceList”,以便每次单击保存按钮时,都会在 ChoiceList 活动的 scrollView 内的 linearLayout 中添加一个新视图。添加的视图“choiceView”应该只是一个包含在 frameLayout 中的 textView。然而,出于某种原因,每次单击保存按钮时,都会将一个新的 ChoiceList 布局添加到 linearLayout 而不是 ChoiceView。我是一个业余的android开发者,刚开始自学如何开发,所以我不知道问题出在哪里。
这是我的 java 和 xml 文件:
public class ChoiceList extends android.app.Activity implements android.view.View.OnClickListener {
private ArrayList<ChoiceView> choiceList;
private Button button;
private LinearLayout choiceListLayout;
private ViewGroup parentGroup;
public void onCreate(Bundle savedInstanceState) {//, List<ChoiceView> list) {
super.onCreate(savedInstanceState);
choiceList = new ArrayList<ChoiceView>();
this.getActionBar().setTitle("");
this.getActionBar().setDisplayHomeAsUpEnabled(true); // adds a left-facing caret alongside the app icon and enables it as an action button
// such that when the user presses it, the activity receives a call to
// onOptionsItemSelected(). The ID for the action is android.R.id.home.
this.initLayout();
}
public void initLayout() {
this.setContentView(R.layout.activity_choice_list); // set up initial layout
choiceListLayout = (LinearLayout) findViewById(R.id.choiceListLayout);
parentGroup = (ViewGroup) choiceListLayout;
button = (android.widget.Button) findViewById(R.id.saveButton);
button.setOnClickListener(new android.view.View.OnClickListener() { // when "Save" button is clicked
@Override
public void onClick(View v) {
newChoiceView(); // Makes new ChoiceView with text from EditText
}
});
}
/**
* Makes and displays new ChoiceView with text from EditText
* Called within InitLayout from clicking Save button
*/
public void newChoiceView() {
ChoiceView cv = new ChoiceView(this.getApplicationContext());
choiceList.add(cv); // add ChoiceView to list of ChoiceViews
choiceListLayout.addView(cv); // display ChoiceView
}
...
}
以及相关的xml...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/choiceScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/choiceListLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<EditText
android:id="@+id/enterChoice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/enterChoiceText" />
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/save"
android:textColor="@color/white" />
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/spinButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/spin" />
</RelativeLayout>
以及我尝试添加到滚动条的视图...
public class ChoiceView extends RelativeLayout {
protected TextView tv;
private String choiceString; // choice text
private EditText et; // editText from which text comes from
/**
* @param context
*/
public ChoiceView(Context context) {
super(context);
LayoutInflater choiceViewInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View choiceView = choiceViewInflater.inflate(R.layout.choice_view, this, true);
LayoutInflater choiceListInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View choiceList = choiceListInflater.inflate(R.layout.activity_choice_list, this, true);
et = (EditText) choiceList.findViewById(R.id.enterChoice); // finds EditText from which string heeds
tv = (TextView) choiceView.findViewById(R.id.choiceViewTextView); // finds TextView to display text
tv.setText(et.getText().toString());
requestLayout();
}
}
及其关联的xml...
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/whatever" >
<TextView
android:id="@+id/choiceViewTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/spin"
android:padding="5dp" />
</FrameLayout>
如果您需要更多信息,请告诉我。
【问题讨论】:
-
你为什么要在你的 ChoiceView 类中增加一个choiceList 布局?
-
老实说,我在 ChoiceView 类中为 ChoiceList 布局添加了一个充气器,因为有人告诉我需要这样做才能获得 editText 信息。这是一个糟糕的提示吗?
-
抱歉回复晚了。是的,因为当你膨胀它时,它会呈现一个全新的选择列表布局,它与原始选择列表完全分开。这就是你遇到问题的原因。尽管如此,您可能应该像其他人所说的那样使用 ListView,因为它更加优化。尽管在某些情况下您可能希望这样做。
-
这是有道理的。但是在不增加布局的情况下,如何找到 EditText id?如果我摆脱 ChoiceList 布局充气器,我的 EditText 变为空(请参阅我以前的论坛问题)。
-
修改 ChoiceView 的构造函数以将 EditText 作为参数。然后,在 ChoiceList 类中,使用
EditText et = (EditText) findViewById(R.id.enterChoice);并在创建时将其传递给任何选择视图。
标签: android android-linearlayout