gpt4 book ai didi

java - 如何在循环中从 EditText 中提取文本并将其连接起来以在单个 TextView 中查看它?

转载 作者:行者123 更新时间:2023-12-01 16:49:50 25 4
gpt4 key购买 nike

希望你们在这段时间一切顺利。我在编程方面有点菜鸟。所以,我有 2 个 EditText 字段,我想从中提取文本。我知道我可以在 View 上调用 getText() 并以编程方式将其设置为 TextView,但我有一个按钮可以在其下方添加更多 EditText 字段,并且我希望当用户单击时应从所有字段中提取文本在单个 TextView 中以特定格式生成并显示。

如果您能帮助我使用 TextWatcher 实现相同的功能,这样用户就无需单击“生成”,我将非常感激

附上代码供引用。

public class BookActivity extends AppCompatActivity {

static final String citationType = "Citation Type:";
TextView authorFirstTv;
int baseOfAuthor = 1;
LinearLayout authorContainerEt;
Button addButton;
Button removeButton;
EditText authorFirstEt;
EditText authorLastEt;
TextView citationHeaderTv;
Button generateButton;

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

Intent citationIntent = getIntent();
String citation = citationIntent.getStringExtra("citation");
final String citationView = citationType + "\u0020" + citation;

TextView citationTextView = findViewById(R.id.citation_header_book_tv);
citationTextView.setText(citationView);

authorFirstEt = findViewById(R.id.book_author_first_tv);
authorLastEt = findViewById(R.id.book_author_last_tv);
citationHeaderTv = findViewById(R.id.book_header_tv);
generateButton = findViewById(R.id.generate_button);
authorContainerEt = findViewById(R.id.book_author_container_ll);
generateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
generateCitation();
}
});

// Add button functionality
addButton = findViewById(R.id.book_add_button);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addAuthor();
String text = Integer.toString(baseOfAuthor);
Toast.makeText(BookActivity.this, text, Toast.LENGTH_SHORT).show();
}
});

// Remove button functionality
removeButton = findViewById(R.id.book_remove_button);
removeButton.setEnabled(false);
removeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (baseOfAuthor >= 2) {
removeButton.setEnabled(true);
removeAuthor();
String text = Integer.toString(baseOfAuthor);
Toast.makeText(BookActivity.this, text, Toast.LENGTH_SHORT).show();
} else {
removeButton.setEnabled(false);
Toast.makeText(BookActivity.this, "Can't remove default author", Toast.LENGTH_SHORT).show();
}
}
});


// For APA
TextView publisherCityAPA = findViewById(R.id.book_city_apa_tv);
TextView publisherStateAPA = findViewById(R.id.book_state_apa_tv);
publisherCityAPA.setVisibility(View.GONE);
publisherStateAPA.setVisibility(View.GONE);

// For Harvard
TextView bookVolumeTv = findViewById(R.id.book_volume_tv);
TextView pagesTv = findViewById(R.id.book_pages_tv);

if (citation.equals("APA")) {
publisherCityAPA.setVisibility(View.VISIBLE);
publisherStateAPA.setVisibility(View.VISIBLE);
}
if (citation.equals("Harvard")) {
publisherStateAPA.setVisibility(View.GONE);
bookVolumeTv.setVisibility(View.GONE);
pagesTv.setVisibility(View.GONE);
}
}

/**
* Method to add a new author field to the list keeping the color coding and dimensions as
* close as possible.
*/
public void addAuthor() {
authorFirstEt = new EditText(getApplicationContext());
authorLastEt = new EditText(getApplicationContext());
baseOfAuthor += 1;
authorContainerEt.addView(authorFirstEt, baseOfAuthor);
baseOfAuthor += 1;
authorContainerEt.addView(authorLastEt, baseOfAuthor);
authorFirstEt.setHint("First Name");
authorLastEt.setHint("Last Name");

// To set the dimensions of the EditText and refresh the layout
authorFirstEt.setWidth(200);
authorLastEt.setWidth(200);
authorFirstEt.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
authorLastEt.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(8, 8, 8, 8);
authorFirstEt.setLayoutParams(layoutParams);
authorLastEt.setLayoutParams(layoutParams);
authorFirstEt.getLayoutParams().width = 305;
authorLastEt.getLayoutParams().width = 305;
authorFirstEt.requestLayout();
authorLastEt.requestLayout();

// To set the color of the EditText field
authorFirstEt.setTextColor(ContextCompat.getColor(BookActivity.this, R.color.colorBackground));
authorFirstEt.setHintTextColor(ContextCompat.getColor(BookActivity.this, R.color.editTextColorHint));
ColorStateList colorStateList = ColorStateList.valueOf(getResources().getColor(R.color.colorBackground));
ViewCompat.setBackgroundTintList(authorFirstEt, colorStateList);
removeButton.setEnabled(true);

authorLastEt.setTextColor(ContextCompat.getColor(BookActivity.this, R.color.colorBackground));
authorLastEt.setHintTextColor(ContextCompat.getColor(BookActivity.this, R.color.editTextColorHint));
ColorStateList lastColorStateList = ColorStateList.valueOf(getResources().getColor(R.color.colorBackground));
ViewCompat.setBackgroundTintList(authorLastEt, lastColorStateList);
removeButton.setEnabled(true);
}

/**
* Method to remove the author from the list
*/
public void removeAuthor() {
authorContainerEt.removeViewAt(baseOfAuthor);
baseOfAuthor -= 1;
authorContainerEt.removeViewAt(baseOfAuthor);
baseOfAuthor -= 1;
}


public void generateCitation() {
EditText test1 = (EditText) authorContainerEt.getChildAt(0);
String authorFirst = test1.getText().toString();
String authorLast = authorLastEt.getText().toString();
String generatedAuthor = authorLast + ",\u0020" + authorFirst + ".\u0020";
if (baseOfAuthor > 1) {
for (int i = authorContainerEt.getChildCount(); i < 2; i = i - 2) {
EditText currentAuthorLast = (EditText) authorContainerEt.getChildAt(i - 1);
String extraAuthorLast = currentAuthorLast.getText().toString();
EditText currentAuthorFirst = (EditText) authorContainerEt.getChildAt(i - 2);
String extraAuthorFirst = currentAuthorFirst.getText().toString();
generatedAuthor = generatedAuthor + "and\u0020" + extraAuthorFirst + "\u0020" + extraAuthorLast;
}
}
String generatedCitation = generatedAuthor;
citationHeaderTv.setText(generatedCitation);
}

/**
*Inner class to implement TextWatcher on EditText View
*/

// private class GenericTextWatcher implements TextWatcher{
//
// private View view;
// private GenericTextWatcher(View view) {
// this.view = view;
// }
//
// public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
// public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
// String text = charSequence.toString();
// String finalText = null;
// switch(view.getId()){
// case R.id.book_author_first_tv:
// finalText = text + ", ";
//// citationHeaderTv.setText(finalText);
// break;
// case R.id.book_author_last_tv:
// finalText = text + ". ";
//// citationHeaderTv.setText(finalText);
// break;
//
//// case R.id.phone:
//// model.setPhone(text);
//// break;
// }
// citationHeaderTv.setText(finalText);
// }
//
// public void afterTextChanged(Editable editable) {
// }
// }

}

最佳答案

创建一个 EditTextArrayList 或类似的东西,您可以在其中添加新的 EditText 并循环遍历列表以获取生成功能的值?

关于java - 如何在循环中从 EditText 中提取文本并将其连接起来以在单个 TextView 中查看它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61712385/

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