简体   繁体   中英

How do I use kotlin codes to modify text appears in android emulator?

I am trying to build my very first app in android studio, "hello, world." so I start a new project and add 2 lines of codes in the bottom.

fun main(args:Array<String>){ printHello()}
fun printHello () {println ("Hello World")}

(1)How do I use above 2 lines of kotlin codes to see the result in android emulator? (2)In another project, I can modify "0" => "hello, world." from res/layout/fragment_first.xml. But I see neither nor fragment_first?xml so where do I go to modify?

在此处输入图像描述 在此处输入图像描述

package com.example.hello

import android.os.Bundle
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(findViewById(R.id.toolbar))

        findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show()
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return when (item.itemId) {
            R.id.action_settings -> true
            else -> super.onOptionsItemSelected(item)
        }
    }
}

fun main(args:Array<String>){ printHello()}
fun printHello () {println ("Hello World")}

Error 1 在此处输入图像描述

Error 2 在此处输入图像描述

Error 3 在此处输入图像描述

To answer your question:

1.

   fun main(args:Array<String>){ printHello()}  
   fun printHello () {println ("Hello World")}

the println would only show in andorid's log, to show something in the emulator, we can use Toast. https://developer.android.com/guide/topics/ui/notifiers/toasts

    fun main(args:Array<String>){ printHello()}  
    fun printHello () {Toast.makeText(this,"Hello World", Toast.LENGTH_LONG).show()}  
  1. I think your other project is using another android studio's new project template, that's why it has res/layout/fragment_first.xml. you can modify res/layout/activity_main for this project

Full code here with minimal modification to your posted code. You should see the toast message when the app launch.

package com.example.hello

import android.os.Bundle
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(findViewById(R.id.toolbar))

        findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()
        }
        main(arrayOf())// notice the call to the function here
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return when (item.itemId) {
            R.id.action_settings -> true
            else -> super.onOptionsItemSelected(item)
        }
    }

    fun main(args:Array<String>){ printHello()}
    fun printHello () {Toast.makeText(this,"Hello World", Toast.LENGTH_LONG).show()}
}

For building your first app, i would suggest going through the official first app guide, https://developer.android.com/training/basics/firstapp

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