简体   繁体   中英

Displaying variables as text in Android Studio

I'm new to mobile development and I am having trouble displaying variables as text on to the screen.

public class wifiinfo() {
    public string getmac = getBSSID();
}

I have this in my main (blank) activity but I have no clue as to how to code this in with XML. The Android Developer tutorials haven't really helped me. Do I have to index the string in a database, perhaps? Any help would be greatly appreciated.

You can use TextView to write text and display it on the screen.

<TextView
     android:id="@+id/textView"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="center_horizontal"
     android:text="Your text here..."/>

If your text line is so long you should write it in res/values/strings.xml:

<string name="my_text">This is a very long line text I want to show on screen.</string>

And use android:text="@string/my_text" to get it in the TextView.

Hope it helps!

Use Textview to display text.

Add this in your XML:

<TextView
     android:id="@+id/textViewSSID"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="center_vertical"
     android:hint="Your SSID"/>

And display text in activity like this:

TextView tvSSID = (TextView) findViewById(R.id.textViewSSID);
String ssid = getBSSID();

tvSSID.setText(ssid);

Why you create an inner class to keep the value of ssid is a real mystery to me. You can make the variable getmac as a member variable in the activity and initialize it in your onCreate of your activity.

Update:

As to your comment, your activity should start somewhat like this:

public class ActivityName extends Activity {

   private string MacId = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_layout);

        MacId = getBSSID();

       //Other functions
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM