An open API service indexing awesome lists of open source software.

https://github.com/queeniecplusplus/android_review_8

Intent to Activity or Context
https://github.com/queeniecplusplus/android_review_8

android intent kotlin

Last synced: about 2 months ago
JSON representation

Intent to Activity or Context

Awesome Lists containing this project

README

        

# Android_Review_8
Intent to Activity or Context 意圖分享純文本,
使用 IntentBuilder 取代 Intent

1. design layout with UI element. And add on Guideline to make a space between ImageView & ImageButton.

//activity_main.xml





























2. define dimens.

// go to app/src/main/res/values/dimens.xml




16dp

3. define customed color.

// go to app/src/main/res/values/colors.xml





#ffffff
#6ab343
#99000000

4. define resorces in app/src/main/res/drawable.

5. design menu resource.

// go to app/src/main/res/menu
// main_menu.xml






6. to create a R.string which is located in file called string.xml under dir res/values/.


Kates App
$
0
Garmin Watch
I\'ve clicked %1$d Desserts for a total of %2$d$ #AndroidDessertClicker
Share
Sharing Not Available

7. source line of code.

package com.example.android.katesapp

[default module]
import android.os.bundle
import androidx.appcompat.app.AppCompatActivity

[databind module]
import androidx.databinding.DataBindingUtil
import com.example.android.katesapp.databinding.ActivityMainBinding

[intent module]
import androidx.core.app.ShareCompat

[system error handler module]
import android.content.ActivityNotFoundException

[menu module]
import android.view.Menu
import android.view.MenuItem

[Toast module]
import android.widget.Toast

class MainActivity: AppCompatActivity(){


// 類別中的共用變數
private var price = 0
private var volume = 0
private lateinit var binding: ActivityMainBinding


// 製作資料型別
// data class garminWatchToBuy(val)

// create a Data Type
// Data Class
// it includes the resource id.
data class garmin3C(val imgId: Int, val price: Int, val volumn: Int)

// 建立集合裝載資料型別的成員
// create a List
// 拆解集合為成員
private val allGarmin3C = listOf(

garmin3C(R.drawable.watchA, 6990, 5),
garmin3C(R.drawable.watchB, 7990, 15),
garmin3C(R.drawable.watchC, 19900, 20)


)


// 提取集合中的成員,成員有 id、價格、數量等屬性
private var currentWatch = allGarmin[0]

override fun onCreate(savedInstanceState: Bundle?){

super.onCreate(savedInstanceState)

// 綁定 UI 元件
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

// 顯示 UI 元件
binding.imgB.setOnClickListner {
clickBuyHandler()
}

binding.imgB.setImageResource(currentWatch.imgId)

binding.price = price
binding.volumn = volumn


}

private fun clickBuyHandler() {

// update value of volumn when user click on the button to demonstrate he/she wanna buy it
volumnAfterBuy--

binding.volumn = volumnAfterBuy

showOtherWatch()


}

private fun showOtherWatch() {

// TODO
// for-in loop
// use the data class called allGramin3C
// retrieve the gramin3C data by creating a varibale calling allGramin3C[0]
// see Anroid_Review_7

}

// Menu to be inflated
override fun onCreateOptionsMenu(menu: Menu): Boolean {

menuInflater.inflate(R.menu.main_menu, menu)
return super.onCreateOptionsMenu(menu)

}

// Items to be selected
override fun onOptionsItemSelected(item: MenuItem): Boolean {

// lambda
// (itemId) -> {body}
// {itemId -> body}
when(item.itemId){
R,id.showMenuTouch -> showGarmin()
}
return super.onOptionsItemSelected(item)

}

// ShareCompat is a public class
// its IntentBuilder is a constructor
// then call its methods and properties
// Intent
// Exception Handler
private fun showGarmin() {

// Intent
// setText(CharSequence text)
// Set the literal text data to be sent as part of the share.
// Question on getIntent() instead of .intent

val showIntent = ShareCompat.IntentBuilder.from(this).setText(getString(R.string.share_text, price, volumn)).setType("text/plain").intent

try {

startActivity(showIntent)

} catch(ex: ActivityNotFoundException) {

Toast.makeText(this, getString(), Toast.LENGTH_LONG).show()

}

}


}

8. today's tip (getString())

https://cloud.tencent.com/developer/ask/72642

9. today's tip (field)

%1$d , the first filed that holds integer type variable
%2$s , the second field that holds string type varibale

https://stackoverflow.com/questions/3656371/is-it-possible-dynamically-to-add-string-to-string-xml-in-android

https://blog.csdn.net/wufen1103/article/details/7846691

10. today's tip (constructor)

1. constructor can't be called directly.

2. constructor has no param.

3. before instance is created, we use constructor instad to ref the members.

https://stackoverflow.com/questions/4506990/what-is-the-use-of-static-constructors

11. today's tip (CharSequence & String)

https://stackoverflow.com/questions/3600635/what-is-the-difference-between-charsequence-and-a-string

12. android's tip (IntentBuiler 意圖建構器)

1. 先導入模組 androidx.core.app.ShareCompat.IntentBuilder。

2. 我們使用此模組,目的是建造一個 IntentBuilder 的型別的實例。

3. 因建構子無法單獨使用,故先呼叫公共類別 SharCompat 與建構子 .IntenBuilder。

4. 建構子成立後,可呼叫內部成員方法,如

setText(@Nullable text: CharSequence?): IntentBuilder

setType(@Nullable mimeType: String?): IntentBuilder

setStream(@Nullable streamUri: Uri?): IntentBuilder

// HTML markup

setHtmlText(@Nullable htmlText: String?): IntentBuilder

------------------------------------------------------------------

from(@NonNull activity: Activity): IntentBuilder

startChooser(): void

getInetent(): Intent

setSubject(@Nullable subject: String?): IntentBuilder

setEmailBcc(@Nullable addresses: Array?): IntentBuilder

setEmailTo(@Nullable addressess: Array?): IntentBuilder

------------------------------------------------------------------

https://developer.android.com/reference/kotlin/androidx/core/app/ShareCompat.IntentBuilder


13. android's tip (Intent 意圖)


------------Main2Activity.java--------------------

//使用 putExtra(), getExtra() 夾帶資料

EditText et;
et = fincdViewById(R.id.edit_text);
// et.getText().toString() or getString(et)
String Msg = et.getString();
Intent anotherIntent = new Intent(MainActivity.this, Main2Activity.class);
anotherIntent.putExtra("I luv android since 2018", msg)
// setResult(2, anotherIntent);
startActivity(anotherIntent);

------------MainActivity.java-----------------------

// onCreate()
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivityForResult(intent, 2)

// onActivityResult()
// if requestCode ==2
String msg = data.getStringExtra("");
txv.setText(msg)


https://medium.com/@skywing0527/android自學筆記-14-intent用法-範例練習-ad774d2f92ec