Android Studio 中的 findViewByID 无法使用 EditText
findViewByID in Android Studio not working with EditText
我似乎无法 findViewById()
使用 EditText。它与按钮一起工作正常,但我认为我做错了什么而且我似乎无法在任何地方找到解决方案。 findViewById()
如何与 EditText 一起使用?
这是我的 class:
public class MainActivity extends ActionBarActivity {
Button sbutton;
EditText book, chapter, verse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sbutton = (Button) findViewById(R.id.sbutton);
book = (EditText) findViewByID(R.id.book1);
chapter = (EditText) findViewByID(R.id.chapter1);
verse = (EditText) findViewByID(R.id.verse1);
}
}
这是我的 XML:
<TextView android:text="Please enter your favorite scripture" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/book1"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chapter1"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/verse1"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/sbutton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="77dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Book"
android:id="@+id/textView2"
android:layout_alignTop="@+id/book"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chapter"
android:id="@+id/textView3"
android:layout_below="@+id/book"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Verse"
android:id="@+id/textView4"
android:layout_below="@+id/chapter"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
我知道这可能不是最好的代码,因为我是 Android Studio 的新手。如果能帮助我理解这种方法的工作原理以及我在这里可能做错了什么,我将不胜感激。谢谢!
你这样做是 "findViewByID()" 但它的 findViewById()
sbutton = (Button) findViewById(R.id.sbutton);
book = (EditText) findViewByID(R.id.book1);
chapter = (EditText) findViewByID(R.id.chapter1);
verse = (EditText) findViewByID(R.id.verse1);
正确的方法名称是
findViewById()
sbutton = (Button) findViewById(R.id.sbutton);
book = (EditText) findViewById(R.id.book1);
chapter = (EditText) findViewById(R.id.chapter1);
verse = (EditText) findViewById(R.id.verse1);
我似乎无法 findViewById()
使用 EditText。它与按钮一起工作正常,但我认为我做错了什么而且我似乎无法在任何地方找到解决方案。 findViewById()
如何与 EditText 一起使用?
这是我的 class:
public class MainActivity extends ActionBarActivity {
Button sbutton;
EditText book, chapter, verse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sbutton = (Button) findViewById(R.id.sbutton);
book = (EditText) findViewByID(R.id.book1);
chapter = (EditText) findViewByID(R.id.chapter1);
verse = (EditText) findViewByID(R.id.verse1);
}
}
这是我的 XML:
<TextView android:text="Please enter your favorite scripture" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/book1"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="60dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/chapter1"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/verse1"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/sbutton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="77dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Book"
android:id="@+id/textView2"
android:layout_alignTop="@+id/book"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chapter"
android:id="@+id/textView3"
android:layout_below="@+id/book"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Verse"
android:id="@+id/textView4"
android:layout_below="@+id/chapter"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
我知道这可能不是最好的代码,因为我是 Android Studio 的新手。如果能帮助我理解这种方法的工作原理以及我在这里可能做错了什么,我将不胜感激。谢谢!
你这样做是 "findViewByID()" 但它的 findViewById()
sbutton = (Button) findViewById(R.id.sbutton);
book = (EditText) findViewByID(R.id.book1);
chapter = (EditText) findViewByID(R.id.chapter1);
verse = (EditText) findViewByID(R.id.verse1);
正确的方法名称是
findViewById()
sbutton = (Button) findViewById(R.id.sbutton);
book = (EditText) findViewById(R.id.book1);
chapter = (EditText) findViewById(R.id.chapter1);
verse = (EditText) findViewById(R.id.verse1);