gpt4 book ai didi

java - 通过单击先前复制的按钮复制按钮并编辑文本,ANDROID

转载 作者:行者123 更新时间:2023-11-30 00:10:21 26 4
gpt4 key购买 nike

我的代码有一个具体问题,我访问过的大多数交易都无法解决。

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

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

我退出了 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);

}



}

最佳答案

您可能不知道 View 的 id,但是,您可以在创建组件时设置标签并通过 findViewWithTag 搜索该组件

喜欢:

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

关于java - 通过单击先前复制的按钮复制按钮并编辑文本,ANDROID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48238370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com