How can I declare TextView as global variable to use in other class

前端 未结 4 1891
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 05:45

I am a new android dev. I want to use a same TextView in all activity. But, i don\'t know how to declare TextView as global Variable & How can i use thi

相关标签:
4条回答
  • 2021-01-16 06:01

    You can use this everywhere:

    TextView textview = (TextView) findViewByID(Your ID);
     textview = (TextView) findViewByID(Your ID);
    
    0 讨论(0)
  • 2021-01-16 06:13

    Write your XML code of text view with id as:

    <TextText
        android:id="@+id/textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    

    Than in your activity declare it before onCreate() method as:

    public static TextView textview = (TextView) findViewByID(Your ID);
    

    Than this will be accessible to all calsses.

    0 讨论(0)
  • 2021-01-16 06:14

    I think this Link this is helpfull to

    http://alinberce.wordpress.com/2012/02/20/android-edittext-with-custom-font-and-clear-button/

    you have to create Editbox just One time & use anywhere in Application

    weather use in Layout file or use Dynamically in java Class

    0 讨论(0)
  • 2021-01-16 06:21

    Make it a singleton. Or just keep one public static reference.

    public class MyReference {
        public static TextView myTextView = new TextView();
    }
    

    and then you can use it anywhere by calling MyReference.myTextView

    0 讨论(0)
提交回复
热议问题