通过单击以前复制的按钮ANDROID复制按钮并编辑文本

时间:2018-01-13 08:57:43

标签: java android jquery android-edittext

我的代码存在一个特定问题,我访问过的大多数交易都无法解决。

从我的应用程序中,我想创建一个用户可以通过单击“添加新类”按钮创建新类的情况,完成后,创建一个edittext和另一个按钮,用户也可以单击新创建的按钮以添加新类,该类也创建新的编辑文本字段,另一个按钮应该是用户希望继续添加新类。这里的挑战是

现在,当用户点击“添加新类”按钮时,会生成多个按钮作为代码中相同数量的数组,这不是我想要实现的。谢谢

我退出了Android开发和整个Java编程语言的新手

我的SchoolSearch设置课程

package com.example.demeainc.demea;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.view.View;
import android.widget.Toast;


public class SchoolSearchSetup extends AppCompatActivity implements View.OnClickListener {

    private EditText searchSchoolID;
    private Button searchSchoolButtonID;
    private ListView listOfSchoolsID;
    private Button  openNewschoolID;
    private LinearLayout schoolSetupLayout;
    private Button addNewClass;
    private EditText classNameEditText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_school_search_setup);
        findAllViewsID();
        initializeListenners();
    }

    public void findAllViewsID(){

        classNameEditText = findViewById(R.id.classNameText);
        addNewClass = findViewById(R.id.addNewClassButton);
        schoolSetupLayout = findViewById(R.id.schoolSetupLayout);
        searchSchoolID = findViewById(R.id.searchSchoolID);
        searchSchoolButtonID = findViewById(R.id.searchSchoolButtonID);
        listOfSchoolsID = findViewById(R.id.listOfSchoolsID);
        openNewschoolID = findViewById(R.id.openNewschoolID);


    }

    public void initializeListenners(){
        openNewschoolID.setOnClickListener(SchoolSearchSetup.this);
        addNewClass.setOnClickListener(SchoolSearchSetup.this);

    }



    @Override
    public void onClick(View view) {

        switch(view.getId()){

            case R.id.openNewschoolID:

               displaySchoolSetUpForms();

               break;

           case R.id.addNewClassButton:

               addNewClassSetUp();


        }

    }


    private void addNewClassSetUp() {

        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);

        LinearLayout.LayoutParams layoutParams = new
                LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        layout.setLayoutParams(layoutParams);

        layout.setPadding(0, 5, 0, 0);

        layout.setGravity(Gravity.CENTER);

        schoolSetupLayout.addView(layout);


        LinearLayout.LayoutParams editTextParams =
                new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        EditText addClassEditTex = new EditText(this);

        addClassEditTex.setHint("Class name, e.g Grade one");

        addClassEditTex.setLayoutParams(editTextParams);

        schoolSetupLayout.addView(addClassEditTex);


        LinearLayout.LayoutParams addNewClassButtonParams =
                new LinearLayout.LayoutParams(200, 60);

        String[] arrayClass = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
                "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45",
                "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60"};

        int length = arrayClass.length;

        Button[] addNewClassBtn = new Button[length];


        for (int i = 0; i < length; i++){


         addNewClassBtn[i] = new Button(this);


        addNewClassBtn[i].setGravity(Gravity.CENTER);

        addNewClassBtn[i].setBackgroundResource(R.color.colorPrimaryDark);

        addNewClassBtn[i].setText("Add New Class");

        addNewClassBtn[i].setTextColor(Color.WHITE);

        addNewClassBtn[i].setLayoutParams(addNewClassButtonParams);

        schoolSetupLayout.addView(addNewClassBtn[i]);

            addNewClassBtn[i].setOnClickListener(handleOnClick(addNewClassBtn[i]));



    }

        String schoolSetupContent = String.valueOf(schoolSetupLayout);

        if (!schoolSetupContent.isEmpty()) {

            Toast.makeText(this, "New class field created scroll down to view", Toast.LENGTH_LONG).show();

        } else {

            Toast.makeText(this, "Class creation failed", Toast.LENGTH_LONG).show();

        }


    }

    View.OnClickListener handleOnClick(final Button button) {
        return new View.OnClickListener() {
            public void onClick(View v) {
                //dont get what else to do here

            }
        };
    }

    private void displaySchoolSetUpForms() {

        schoolSetupLayout.setVisibility(View.VISIBLE);

    }



}

1 个答案:

答案 0 :(得分:1)

您可能不知道视图的ID,但是,您可以在创建组件时设置标记,并按findViewWithTag

搜索该组件

像:

  Button btn = (Button)findViewWithTag("btn_send");