2012-10-05 31 views
12

tôi có một main.xml layout xml như thế nàyKhai EditText như TextView trong Android

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="fill_parent" 
android:gravity="center" > 
<EditText  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="text" 
android:textColor="#000" 
android:textSize="16dp" 
android:id="@+id/edtxt" 
android:gravity="center|top|left"/>  
</RelativeLayout> 

và tôi tuyên bố này trong hoạt động của tôi như thế này

public class MyActivity extends Activity { 
private TextView txt = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    txt= (TextView)findViewById(R.id.edtxt); 
} 

} 

nhưng tôi không có vấn đề trong hoạt động của tôi . tại sao?

Trả lời

2

Câu trả lời rất đơn giản, vì vậy là vì EditText mở rộng TextView.

4

Điều đó có thể do EditText là một phân lớp của TextView. Bạn đang soạn một EditText thành TextView, hoạt động vì EditText IS là TextView.

Kiểm tra tài liệu của EditText: http://developer.android.com/reference/android/widget/EditText.html

Và đây là một chút gì đó để bạn có thể tìm hiểu về thừa kế, đó là những gì đang xảy ra ở đây: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

Các vấn đề liên quan