简体   繁体   English

以编程方式将视图添加到滚动视图内的相对布局

[英]Adding views programatically to a relative layout within a scrollview

Please bear with me as I am new to the use of Views and Layouts. 请接受我的意见,因为我是使用视图和布局的新手。

I am trying to create an application in which the user can enter a text field, press a button and have a new text field appear and they can continue adding text fields in this way. 我正在尝试创建一个应用程序,用户可以在其中输入文本字段,按下按钮并显示新的文本字段,并且他们可以以此方式继续添加文本字段。

My solution was to have the top level be a scrollview and then have a relative view as a child within that(this way I can then programatically insert more editviews in my code with the OnClick() listener. 我的解决方案是让顶层成为滚动视图,然后在其中作为一个相对视图(这样,我便可以使用OnClick()侦听器以编程方式在我的代码中插入更多editviews。

I have seen and read a couple of other posts pertaining to relative views but it seems there is still something that I am missing. 我已经阅读并阅读了其他几篇与相对观点有关的文章,但似乎仍然缺少某些内容。 I have tried 我努力了

Here is the xml 这是XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_create_accounts"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nic.mybudget.CreateAccountsActivity">


<RelativeLayout
    android:id="@+id/activity_create_accounts_relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/activity_name"
        android:inputType="textAutoComplete"
        android:layout_alignParentTop="true"
        android:id="@+id/activity_createAccounts_relativeLayout_activityName"/>


    <Button
        android:text="+"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/activity_createAccounts_relativeLayout_activityName"
        android:id="@+id/activity_create_accounts_relativeLayout_activityName_addButton" />

    <Button
        android:text="Save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/activity_create_accounts_relativeLayout_activityName_addButton"
        android:layout_centerHorizontal="true"
        android:id="@+id/activity_create_accounts_relativeLayout_activityName_saveButton" />


</RelativeLayout>

And here is the Code where I try to add new editviews. 这是我尝试添加新editviews的代码。

public class CreateAccountsActivity extends Activity {

static private final String TAG = "MAIN-Activity";
int numAccounts = 0;
int lastAccountID;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_accounts);

    final RelativeLayout Relative = (RelativeLayout) findViewById(R.id.activity_create_accounts_relativeLayout);
    final TextView oldAccount = (TextView) findViewById(R.id.activity_createAccounts_relativeLayout_activityName);
    final TextView newAccount = new TextView(this);

    final Button addNewAccountButton = (Button) findViewById(R.id.activity_create_accounts_relativeLayout_activityName_addButton);
    addNewAccountButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.i(TAG, "addNewAccountOnClick");
            numAccounts = numAccounts+1;
            int newAccountID = oldAccount.getId() + numAccounts;

            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            newAccount.setLayoutParams(rlp);
            newAccount.setHint("Hint" );
            newAccount.setId(newAccountID);
            Relative.addView(newAccount);

            RelativeLayout.LayoutParams blp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            blp.addRule(RelativeLayout.BELOW, newAccountID-1);
            addNewAccountButton.setLayoutParams(blp);
        }
    });
}

}

As you can see what I am trying (and failing) to do is add the new edit view at the top of the page and simply push everything else down the page. 如您所见,我正在尝试(但失败)的是在页面顶部添加新的编辑视图,然后将其他所有内容向下推。 What am I getting wrong here with the relative layout? 我在这里相对布局有什么问题?

Any help is appreciated. 任何帮助表示赞赏。

First thing View with id activity_createAccounts_relativeLayout_activityName is EditText and you are casting it with TextView so that is wrong cast it to EditText . 第一件事,查看ID为activity_createAccounts_relativeLayout_activityNameEditText和你用铸造它TextView ,这样是错投给EditText

And to your actual problem: You are using same EditText instance with variable newAccount and adding it again in relative layout if you want to add one more EditText in relative layout you have to initialise EditText inside onclicklistener. newAccount您的实际问题:您正在使用具有变量newAccount相同EditText实例,并在相对布局中再次添加它,如果要在相对布局中再添加一个EditText,则必须在onclicklistener内初始化EditText。 Just add one line newAccount= new EditText(context) in your onclicklistener code before line numAccounts = numAccounts+1; 只需在您的onclicklistener代码中添加一行newAccount= new EditText(context) ,然后再添加numAccounts = numAccounts+1;numAccounts = numAccounts+1;

Happy Coding ! 编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM