我试图将EditText值添加到ArrayList并显示到TextView,但我一直在' null'在电视里我尝试了几件事,甚至看过其他项目的代码,这些代码工作......我不知道出了什么问题......
package com.wizardpower.tester;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
String proContainer;
EditText etInput;
TextView tvOutput;
Button bButton;
List <String> proStrings = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < proStrings.size(); i++){
proContainer = proStrings.get(i);
}
bButton = (Button) findViewById(R.id.bButton);
tvOutput = (TextView) findViewById(R.id.tvOutputTxt);
etInput = (EditText) findViewById(R.id.etInput);
bButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bButton:
proStrings.add(etInput.getText().toString());
tvOutput.setText(String.valueOf(proContainer));
break;
}
}
}
这是XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/MainActivity">
<TextView android:text="OutputText" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvOutputTxt"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/etInput"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:hint="add your text here" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter"
android:id="@+id/bButton"
android:layout_below="@+id/etInput"
android:layout_centerHorizontal="true"
android:layout_marginTop="105dp" />
答案 0 :(得分:1)
这是你应该做的:
StringBuilder proContainer;
List <String> proStrings = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bButton = (Button) findViewById(R.id.bButton);
tvOutput = (TextView) findViewById(R.id.tvOutputTxt);
etInput = (EditText) findViewById(R.id.etInput);
proContainer=new StringBuilder();
bButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.bButton:
proStrings.add(etInput.getText().toString());
for (int i = 0; i < proStrings.size(); i++){
proContainer.append(proStrings.get(i));
}
tvOutput.setText(proContainer.toString());
break;
}
}