Java活用生活

ログイン

オンライン状況

オンラインユーザー1人
ログインユーザー0人
登録ユーザー2人

カウンタ

COUNTER332256

日誌

Kotlin事始め
1234
2023/05/14

文字コードを出力する

固定リンク | by:aramaki
# 文字コードを出力
import sys
print(sys.getfilesystemencoding())

21:33 | 投票する | 投票数(0)
2022/06/21

コルーチン

固定リンク | by:aramaki
コルーチンモジュールを登録
--------------------------------------------------
build.gradle.ktsに以下追加
dependencies {

implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2")
}

fun execCoroutine(){
runBlocking{
    launch {
        delay(1000L)
        println("Yamada")
    }
    println("My Sample is")
}

fun main(args: Array<String>) {
// コルーチン
    execCoroutine()



}


サンプル実行結果
My Sample is
Yamada

23:56 | 投票する | 投票数(0)
2022/06/18

非推奨のstartActivityForResultの対応

固定リンク | by:aramaki
非推奨のstartActivityForResultの対応
-------------------------------------------------------
gladle
 //Android Result API用
 implementation 'androidx.activity:activity-ktx:1.4.0'

実装コード
++++++++++++++
private fun preview(){
    Intent(MediaStore.ACTION_IMAGE_CAPTURE).also {
        intent -> intent.resolveActivity(packageManager)?.also {
   // カメラ起動
          //  startActivityForResult(intent,REQUEST_PREVIEW) // startActivityForResultは非推奨
            startActivityResult.launch(intent)
        }
    }

}

private final val startActivityResult=registerForActivityResult(ActivityResultContracts.StartActivityForResult()){

    result:ActivityResult ->
    if(result.resultCode==Activity.RESULT_OK){
        // カメラで取得した画像をImageViewに表示する
        val imageBitmap=result.data?.extras?.get("data") as Bitmap
        binding.imageView.setImageBitmap(imageBitmap)
    }
}
13:18 | 投票する | 投票数(0)
2022/06/13

タイマーフォーマット

固定リンク | by:aramaki
タイマーフォーマット Kotlin 拡張関数
--------------------------------------
%1 は第1引数 %2は第2引数
binding.timerText.text="%1:%2$02d".format(minute,second)
17:59 | 投票する | 投票数(0)
2021/06/14

Jetpack Compose基本サンプル Checkbox 1.0.0-bata08

固定リンク | by:aramaki
Jetpack Compose基本サンプル Checkbox 1.0.0-bata08
-------------------------------------------------------------------------------
CheckboxのStateの値の変更を参照するText

略)
  val checkedState = remember{ mutableStateOf(false)}
 var message = remember { mutableStateOf("Hello!") }
 var txtState = remember { mutableStateOf("0")}

略)

 Checkbox(
                    checked = checkedState.value,
                   // txtState=checkedState.value  ? 1 : 0
                    onCheckedChange = {
                        checkedState.value = it
                        txtState.value=if(checkedState.value)  mutableStateOf("10").value else mutableStateOf("0").value
                        Log.e("txtState = >>",""+txtState.value)
                    }
                )


略)
Button(
                modifier = Modifier.background(Color.Blue),
                onClick = {
                    val n = txtState.value.toInt()
                    var total = 0
                    for (n in 1..n) {
                        total += n
                    }
                    message.value = "total: $total"
                }
            ) {
                Text(
                    text = "Click",
                    style= TextStyle(
                        color= Color.White,
                        fontSize = 20.sp
                    )
                )
   }
14:15 | 投票する | 投票数(0)
2021/06/14

Jetpack Compose基本サンプル TextField

固定リンク | by:aramaki
Jetpack Compose基本サンプル TextField 1.0.0-bata08
-----------------------------------------------------------------
@Composable
    fun Greeting(){
        var state= remember {
            mutableStateOf("ok")
        }
        //var state= state{
           // TextFieldValue("ok")
        //}
        Column() {
            Text(
                text = "typed:\"" + state.value + "\".",
                style = TextStyle(
                    color = Color.Red,
                    fontSize = 28.sp
                )
            )
            TextField(
                value = state.value ,
                textStyle = TextStyle(fontSize = 20.sp),
                onValueChange = {
                    state.value = it
                }

            )
        }
    }

※State の値が変わることで Composable を再描画する、よって入力するたびにTextFieldに入力値が反映する。

12:45 | 投票する | 投票数(0)
2021/05/30

Jetpack Compose基本サンプル

固定リンク | by:aramaki
Jetpackの基本サンプで、2021 5/30日時点で、エラーなく動くサンプルアプリをを掲載する。書籍サンプルもネットサンプルも動かないため、約1日半費やした。

MainActivity.kt
--------------------------------------------------
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.mysecondcomposeapp.ui.theme.MySecondComposeAppTheme

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {

            MySecondComposeAppTheme {
                Surface(color = MaterialTheme.colors.background) {
                    MainContent()
                }
            }
        }
    }

    @Composable
    fun MainContent(){
        Column(
            Modifier
                .padding(25.dp)
        ) {
            val checkedState = remember { mutableStateOf(false)}

            var message = remember { mutableStateOf("Hello!") }

            var txtState = remember { mutableStateOf("0")}
            Row(
                Modifier.padding(10.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(
                    text = message.value,
                    style= TextStyle(
                        color= Color.Red,
                        fontSize = 28.sp
                    )
                )
                Checkbox(
                    checked = checkedState.value,
                    onCheckedChange = { checkedState.value = it },
                )

                Text(
                    text = "Do you like coffee?",
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier
                        .padding(start = 10.dp),
                )
            }

            var result = "unchecked"
            var textColor = Color.Red
            if (checkedState.value){
                result = "checked"
                textColor = Color.Blue
            }

            Text(
                text = "Checkbox is $result.",
                fontSize = 28.sp,
                fontStyle = FontStyle.Normal,
                fontWeight = FontWeight.Normal,
                fontFamily = FontFamily.SansSerif,
                modifier = Modifier
                    .padding(20.dp),
                textAlign = TextAlign.Center,
                color = textColor
            )
            Button(onClick = {

            }) {
                Text(
                    text = "Click",
                    style= TextStyle(
                        color= Color.White,
                        fontSize = 20.sp
                    )
                )
                val n = txtState.value.toInt()
                var total = 0
                for (n in 1..n) {
                    total += n
                }
                message.value = "total: $total"
            }

        }
    }


    @Preview
    @Composable
    fun ComposablePreview(){
        MainContent()
    }
}

22:55 | 投票する | 投票数(0)
2021/05/21

4.スケジュールアプリ(SQLite版)

固定リンク | by:aramaki
Host Fragment
----------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!--  app:defaultNavHost="true" でBackボタンやUpボタンが連携される-->
    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>



14:14 | 投票する | 投票数(0)
2021/05/21

3.スケジュールアプリ(SQLite版)

固定リンク | by:aramaki
Listでデータ表示するCardViewのレイアウト
-----------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginBottom="10dp"
    app:cardBackgroundColor="#ffc"
    app:cardCornerRadius="7dp"
    app:cardElevation="5dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="16dp" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/title"
                android:gravity="start"
                android:textSize="25sp"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/date"
                android:gravity="end"
                android:textSize="12sp"/>


        </LinearLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/detail"
            android:textSize="15sp"/>



    </LinearLayout>

</androidx.cardview.widget.CardView>
14:05 | 投票する | 投票数(0)
2021/05/17

drawable内のイメージリソースを、イメージ名を指定して取得

固定リンク | by:aramaki
drawable内のイメージリソースを、イメージ名を指定して取得
------------------------------------------------------------------------------------------
// R.drawable.イメージ名
var imageResource=context.resources.getIdentifier(
   BeanのList[position].imageName,
   "drawable",context.packageName
)

08:16 | 投票する | 投票数(0)
1234