i have a xml layout main.xml like this
<?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>
and i declare this in my activity like this
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);
}
}
but i have no problem in my activity. why?
see this link http://developer.android.com/reference/android/widget/EditText.html
and look here
you will find your answer
That would be because EditText is a subclass of TextView. You're casting an EditText to a TextView, which works because an EditText IS a TextView.
Check out the EditText's docs: http://developer.android.com/reference/android/widget/EditText.html
And here's a little something so that you can learn about inheritance, which is what is happening here: http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With