要保存到Integer Arraylist的EditText

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

我想在EditText上获取数字用户输入,我希望它保存到Integer Arraylist。我这里有代码,编号以编程方式添加到名为number的arraylist中。我还添加了Textviews来显示整个数组和数组的第一个元素,看它是否好。

我需要帮助的是将用户输入保存到EditText到同一个Arraylist的语法。我搜索了这个网站,发现什么都不合适。我还希望在Textview中看到结果,以确保其工作。

enter code hereI am trying to get numeric user input on an EditText and I want it to be saved to an Integer Arraylist. I have code here that has numbers added programatically to an arraylist called number. I have also added Textviews to display the entire array and the first element of the array to see if it is good.

我需要帮助的是将用户输入保存到EditText到同一个Arraylist的语法。我还希望在Textview中看到结果,以确保其工作。

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

    ArrayList<Integer> number = new ArrayList<Integer>();

    look = (TextView)findViewById(R.id.tv1);
    look2 = (TextView)findViewById(R.id.tv2);

    setUIViews();

    one.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            display1.setText("1");
        }
    });

    two.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            display1.setText("2");
        }
    });

    three.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            display1.setText("3");
        }
    });

    number.add(1);
    number.add(22);
    number.add(45);
    number.add(17);
    number.add(0,7);

    for (int i=0; i < number.size(); i++){
        look2.setText(look2.getText() +" " + number.get(i) + " , ");
        look.setText("First element is: "+number.get(0));
    }

}

private void setUIViews (){

    one = (Button)findViewById(R.id.btn1);
    display1 = (EditText)findViewById(R.id.et1);

    two = (Button)findViewById(R.id.btn2);
    display1 = (EditText)findViewById(R.id.et1);

    three = (Button)findViewById(R.id.btn3);
    display1 = (EditText)findViewById(R.id.et1);


}

} 在此处输入代码

enter code here<android.support.constraint.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">

<EditText
    android:id="@+id/et1"
    android:layout_width="323dp"
    android:layout_height="58dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.017" />

<Button
    android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="244dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="244dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:inputType="number"
    android:text="1"
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.522"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.212" />

<Button
    android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:inputType="number"
    android:text="2"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.212" />

<Button
    android:id="@+id/btn3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:inputType="number"
    android:text="3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.921"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.212" />

<TextView
    android:id="@+id/tv1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.355" />

<TextView
    android:id="@+id/tv2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.416" />

<Button
    android:id="@+id/btnview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="View Arraylist"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.798" />
enter code here
android arraylist android-edittext
2个回答
0
投票

我需要帮助的是将用户输入保存到EditText到同一个Arraylist的语法。

我想你的意思是:

number.add(Integer.parseInt(display1.getText().toString()));

0
投票

您可以处理EditText.AddonTextChangeListner并将每个'char'转换为Int并将其添加到数组并删除以防删除我认为这将是有效的

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