简体   繁体   English

如何将 Edittext 中的输入值保存到数组中?

[英]how to Save Inputted Values ​from Edittext into Array?

I am making a program to get Values from edittext and store it in an array then I want to display that array on screen and here is my command我正在制作一个程序以从 edittext 获取值并将其存储在一个数组中,然后我想在屏幕上显示该数组,这是我的命令

 EditText ed1,ed2;
    RadioButton rd1,rd2,rd3,rd4,rd5;
    Button btn1;
    TextView tvkq;



String X=ed2.getText().toString();
int soX=Integer.parseInt(X);
String dayso=ed1.getText().toString().trim();
int soN=Integer.parseInt(dayso);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    anhxa();
    btn1.setOnClickListener(view ->  {
        ed1.setText(" ");
        ArrayList<String> arryList = new ArrayList<String>();
        arryList.add(ed1.getText().toString());
        if (rd1.isChecked()){
            tvkq.setText("Kêt quả:"+arryList);
        }

    });

}

When I run the program, I got an error, please help me当我运行程序时,出现错误,请帮助我

Try this尝试这个

public class MainActivity extends AppCompatActivity {公共 class MainActivity 扩展 AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EditText ed1 = findViewById(R.id.et1);
        Button btn1 = findViewById(R.id.btn1);
        TextView tvkq = findViewById(R.id.tvkq);
        
        btn1.setOnClickListener(view ->  {

            ArrayList<String> arryList = new ArrayList<String>();
            arryList.add(ed1.getText().toString());
                tvkq.setText("Kêt quả:"+arryList);
        });
    }
}

And the xml file和 xml 文件

 <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">



    <TextView
        android:id="@+id/tvkq"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tvkq"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <EditText
        android:id="@+id/et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        app:layout_constraintBottom_toTopOf="@id/tvkq"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

First problem in your code will be trying to get values from view without initialisation even before XML file is loaded in your activity.您的代码中的第一个问题是,即使在您的活动中加载 XML 文件之前,也将尝试在不初始化的情况下从视图中获取值。

Solution: You suppose to initialize your views under onCreate() and below setContentView(R.layout.activity_main) methods, by providing correct view Id as following;解决方案:您假设在 onCreate() 和 setContentView(R.layout.activity_main) 方法下初始化您的视图,方法是提供正确的视图 ID,如下所示;

EditText ed1,ed2;
RadioButton rd1,rd2,rd3,rd4,rd5;
Button btn1;
TextView tvkq;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edt1 = findViewById(R.id.edt1);
    edt2 = findViewById(R.id.edt2);
    btn1 = findViewById(R.id.btn1);
    ......//Initialize other views

}

Second you are clearing text from your edit text before adding it to an array and also declare and initialize ArrayList outside click Listener.其次,您在将编辑文本添加到数组之前从编辑文本中清除文本,并在点击侦听器外部声明和初始化 ArrayList。

EditText ed1,ed2;
RadioButton rd1,rd2,rd3,rd4,rd5;
Button btn1;
TextView tvkq;
ArrayList<String> arryList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    edt1 = findViewById(R.id.edt1);
    edt2 = findViewById(R.id.edt2);
    btn1 = findViewById(R.id.btn1)

    ......//Initialise other views
    arryList = new ArrayList<String>();

    btn1.setOnClickListener(view ->  {
        arryList.add(ed1.getText().toString());
        if (rd1.isChecked()){
           tvkq.setText("Kêt quả:"+arryList.toString());
        }
        ed1.setText(" ");
    });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM