java无法从EditText中获取文本
所以我想读取EditText
字段的输入。它不起作用。我调试了它,发现EditText
视图列在那里。但当我把它打印到控制台上时,它是空的。我不知道为什么,因为我确实在里面放了一段文字。而且,身份证是对的,我真的不知道怎么了
以下是EditText
所在的视图:
<?xml version="1.0" encoding="utf-8"?>
<安卓x.constraintlayout.widget.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context=".counters.AddCounter">
<TextView
安卓:id="@+id/txtNewCounter"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:fontFamily="@font/allerta"
安卓:text="@string/addNewCounter"
安卓:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
安卓:id="@+id/btnSaveCounter"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:fontFamily="@font/allerta"
安卓:text="@string/save"
安卓:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
安卓:id="@+id/txtCounterName"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="10dp"
安卓:layout_marginTop="20dp"
安卓:fontFamily="@font/allerta"
安卓:text='@string/newCounterName'
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtNewCounter" />
<EditText
安卓:id="@+id/inpuTextNewCounterName"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="10dp"
安卓:layout_marginTop="5dp"
安卓:layout_marginEnd="10dp"
安卓:contentDescription="@string/name"
安卓:ems="10"
安卓:hint="@string/name"
安卓:inputType="textPersonName"
安卓:singleLine="true"
安卓:text="Cookies don't"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtCounterName" />
<TextView
安卓:id="@+id/txtCounterEntryValue"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="10dp"
安卓:layout_marginTop="20dp"
安卓:fontFamily="@font/allerta"
安卓:text="@string/entryNum"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/inpuTextNewCounterName" />
<EditText
安卓:id="@+id/numEntryNum"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_marginStart="10dp"
安卓:layout_marginTop="10dp"
安卓:layout_marginEnd="10dp"
安卓:ems="10"
安卓:hint="@string/number"
安卓:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtCounterEntryValue" />
</安卓x.constraintlayout.widget.ConstraintLayout>
以下是我评估表单的代码:
public class AddCounter extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_counter);
//Setting save button action listener
findViewById(R.id.btnSaveCounter).setOnClickListener(new ActionListeners().getSaveCounter());
}
public static void evaluateCounter(View view) throws NoNameException, NumberOutOfBoundException {
//Getting context
Context context = view.getContext();
//Counter Name
View viewEdit = View.inflate(context, R.layout.activity_add_counter, null);
EditText counterName = viewEdit.findViewById(R.id.inpuTextNewCounterName);
System.out.println("Cookies fly like:" + counterName.getText().toString() + "!");
//Input to string
String name = counterName.getText().toString();
//If no name was specified exception thrown
if (name.equals("")) {
throw new NoNameException(context.getString(R.string.noNameSpecified));
}
//get entry number
EditText numberView = View.inflate(context, R.layout.activity_add_counter, null).findViewById(R.id.numEntryNum);
//Number
int number = 0;
//If nothing specified
if (!(numberView.getText().toString().equals(""))) {
if (Integer.parseInt(numberView.getText().toString()) > 1000 || Integer.parseInt(numberView.getText().toString()) < -1000) {
throw new NumberOutOfBoundException(context.getString(R.string.numOutOfBound));
}
}
//New Counter Object
Counter counter = new Counter(number, name);
//storing counter
ToJson toJson = new ToJson();
toJson.storeCounter(counter, context);
}
}
正如您在这里看到的,这个if
-语句总是正确的。
要查看文本的外观,我打印了一个文本:
System.out.println("Cookies fly like:" + counterName.getText().toString() + "!");
我确实在输入中输入了一个单词,输出为空,正如您在这里看到的:
I/System.out: Cookies fly like:!
这是我的ActionListener:(
public class ActionListeners {
private OnClickListener saveCounter = v -> {
try {
//Evaluate Counter
AddCounter.evaluateCounter(v);
} catch (NoNameException e) {
//If no name was specified
CounterMethods.makeSnackbar(v, Snackbar.LENGTH_SHORT, HapticFeedbackConstants.REJECT, v.getContext().getString(R.string.noNameSpecified));
e.printStackTrace();
} catch (NumberOutOfBoundException e) {
//If entry is higher or lower than available
CounterMethods.makeSnackbar(v, Snackbar.LENGTH_SHORT, HapticFeedbackConstants.REJECT, v.getContext().getString(R.string.numOutOfBound));
}
};
public OnClickListener getSaveCounter() {
return this.saveCounter;
}
}
如您所见,我在xml文件中将文本设置为“Cookies不能飞行”,但是,如果我在应用程序中将文本更改为其他内容,则输出是我在xml中设置的文本
I/System.out: Cookies fly like:Cookies can't fly!
有什么问题
# 1 楼答案
试试这个