![](/img/trans.png)
[英]how to add array values inputted in one edittext in java eclipse?
[英]how to Save Inputted Values from Edittext into Array?
我正在制作一個程序以從 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);
}
});
}
當我運行程序時,出現錯誤,請幫助我
嘗試這個
公共 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);
});
}
}
和 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>
您的代碼中的第一個問題是,即使在您的活動中加載 XML 文件之前,也將嘗試在不初始化的情況下從視圖中獲取值。
解決方案:您假設在 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
}
其次,您在將編輯文本添加到數組之前從編輯文本中清除文本,並在點擊偵聽器外部聲明和初始化 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.