那么,现在我的 XML 文件中有一个文本元素要求用户输入,我如何将此用户输入放入我的 java 文件中,以便我可以将其用作数字?到目前为止,这是我的 XML。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="@+id/uitgaven"
android:layout_weight="1"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:hint="@string/uitgaven"
/>
<EditText android:id="@+id/inkomsten"
android:layout_weight="1"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:hint="@string/inkomsten"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"
/>
</LinearLayout>
我的java目前基本上是空的,因为我目前对它了解不多。
最佳答案
首先,您必须使用以下方式获取 View
mEditText = (EditText) findViewById(R.id.uitgaven)
或
mEditText = (EditText) findViewById(R.id.inkomsten)
无论您需要哪一个文本。然后您需要做的就是获取文本本身。您可以通过以下方式获得:
String myText = mEditText.getText().toString();
关于java - 如何获取java文件中的XML字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38917890/