动态创建textview并让它显示变量的值

时间:2018-04-16 11:55:50

标签: android variables dynamic textview

我想要完成的是:
每次我点击一个按钮时,它应该创建一个新的TextView,这个新创建的TextView将在我点击该按钮时显示EditText的值。

例如:如果我在EditText中编写“This”并单击Button,它应该创建一个显示“This”的TextView。如果我在同一个EditText中写“works”并按下Button,它应该创建第二个显示“works”的TextView。

让我告诉你我的尝试:

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


    Button b = (Button)findViewById(R.id.erstellButton);
    ll = (LinearLayout)findViewById(R.id.LL);
    layoutParams = new LinearLayout.LayoutParams
            (layoutParams.WRAP_CONTENT, layoutParams.WRAP_CONTENT);

    b.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            et = (EditText) findViewById(R.id.editText3);
            TextView view = new TextView(AusgabeActivity.this);
            ll.addView(view, layoutParams);
            String plsJustWork = et.getText().toString();
            view.setText(++i+"test" +plsJustWork);

        }
    });

现在,如果我使用此代码,每次单击按钮时都会创建一个新的TextView,但它只显示“1test”,下次单击Button时显示“2test”。因此,不显示EditText(editText3 / et)的值。

3 个答案:

答案 0 :(得分:0)

试试这个

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    Button b;
    int i = 0;
    EditText et;
    LinearLayout ll;

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

        et = (EditText) findViewById(R.id.editText3);
        b = (Button) findViewById(R.id.erstellButton);
        ll = (LinearLayout) findViewById(R.id.LL);

        final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams
                (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                String plsJustWork = et.getText().toString();

                if (TextUtils.isEmpty(plsJustWork)) {
                    et.setError("Please Enter Value");
                    et.requestFocus();
                } else {
                    TextView view = new TextView(MainActivity.this);
                    ll.addView(view, layoutParams);

                    view.setText(++i + " test " + plsJustWork);
                    et.setText("");
                }


            }
        });
    }
}

答案 1 :(得分:0)

使用以下代码..

public class Demo extends AppCompatActivity {
LinearLayout ll;
LinearLayout.LayoutParams layoutParams;
EditText et;
int i = 0;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo);
    Button b = (Button) findViewById(R.id.daBtnSubmit);

    ll = (LinearLayout) findViewById(R.id.demo);
    layoutParams = new LinearLayout.LayoutParams
            (layoutParams.WRAP_CONTENT, layoutParams.WRAP_CONTENT);
    et = findViewById(R.id.daEtData);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
       if (!TextUtils.isEmpty(et.getText().toString().trim())) {
                TextView view = new TextView(Demo.this);
                ll.addView(view, layoutParams);
                view.setText(++i + "  test " + et.getText().toString().trim()); // if not create every time then remove i value.
                et.setText("");
                et.setError(null);
            } else {
                et.setError("Please Enter text");
            }
            }
    });
}

}

答案 2 :(得分:0)

您可以使用listView或recylerview来实现相同的目标。这是执行此操作的代码

public class MainActivity extends BaseActivity {
    ArrayList<String> arrayList;
    EditText etEditText;
    Button btButton;
    ListView lvListView;
    ArrayAdapter<String> adapter;

    @Override

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
        setAdapter();
        setClickListener();


    }

    private void setClickListener() {
        btButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!etEditText.getText().toString().isEmpty()) {
                    arrayList.add(etEditText.getText().toString());
                    if (adapter != null)
                        adapter.notifyDataSetChanged();

                }
            }
        });
    }

    private void initUI() {
        etEditText = findViewById(R.id.etEditText);
        btButton = findViewById(R.id.btButton);
        lvListView = findViewById(R.id.lvListView);

    }

    private void setAdapter() {
        arrayList = new ArrayList<>();
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, arrayList);
        lvListView.setAdapter(adapter);

    }


}

添加activity_main是

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/etEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="ClickMe"
        app:layout_constraintTop_toBottomOf="@+id/etEditText" />

    <ListView
        android:id="@+id/lvListView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/btButton" />


</android.support.constraint.ConstraintLayout>