如何在循环中提取EditText中的文本,并将其连接到一个TextView中查看?

问题描述 投票:0回答:1

希望大家在这段时间里都能过得好。我在编程方面是个小菜鸟。所以,我有2个EditText字段,我想从中提取文本。我知道我可以调用视图上的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) {
//        }
//    }

}
java android textview android-edittext textwatcher
1个回答
0
投票

制作一个 ArrayListEditText 或者类似的东西,你可以只添加你的新的EditTexts,并通过列表循环来获取生成功能的值?

© www.soinside.com 2019 - 2024. All rights reserved.