G3503_And_note_コードの作成

2025110

12:59

G3503_andoroid_and_note_project_code_report_2

 

基本的に、コピーペーストで作成します

 

 

 

 

使う予定のグローバル変数をすべてコピーペーストします

 

 

 

// グローバル変数

 

public static string Path_com = @"/storage/emulated/0/Android/data/com.companyname.And_note";

public static string Path_files = "";

public static string Path_key = "";

 

 

public static EditText[] Edt_tbl = new EditText[5];

public static Button[] But_tbl = new Button[20];

 

public int VOICE { get; private set; }

public static string Sel_cst = "";   // 場所の選択名称

public static string Voice_cst = ""; // 音声テキスト

public static string Edit_cst = "";  // エディットテキスト

public static string Gps_cst = "";  // GPS

public static string Gps_cst_tmp = "";  // GPS tmp

public static int Sys_yy_all, Sys_mm, Sys_dd, Sys_th, Sys_tm, Sys_ts;

public static int Dsp_yy_all, Dsp_mm;

 

public static string Type_name = "本日表示";

 

画面の部品を準備します

 

 

 

 

        <EditText

        android:inputType="textMultiLine"

        android:layout_width="match_parent"

        android:layout_height="300.0dp"

        android:id="@+id/editText0"

        android:layout_marginTop="0.0dp"/>

 

 

        <Button

        android:text=""

        android:layout_width="140.0dp"

        android:layout_height="70.0dp"

        android:id="@+id/button0"

        android:layout_marginTop="620.0dp"/>

 

    <Button

android:text=""

android:layout_width="140.0dp"

android:layout_height="70.0dp"

android:id="@+id/button1"

android:layout_marginTop="620.0dp"

android:layout_marginRight="0.0dp"

android:layout_marginLeft="200.0dp"

/>

 

                <Button

android:text=""

android:layout_width="140.0dp"

android:layout_height="70.0dp"

android:id="@+id/button2"

android:layout_marginTop="620.0dp"

android:layout_marginRight="0.0dp"

android:layout_marginLeft="400.0dp"

/>

 

        <Button

        android:text=""

        android:layout_width="570.0dp"

        android:layout_height="45.5dp"

        android:id="@+id/button3"

        android:layout_marginTop="550.0dp"/>

 

        <Button

        android:text=""

        android:layout_width="570.0dp"

        android:layout_height="45.5dp"

        android:id="@+id/button4"

        android:layout_marginTop="500.0dp"/>

 

        <Button

        android:text=""

        android:layout_width="570.0dp"

        android:layout_height="45.5dp"

        android:id="@+id/button5"

        android:layout_marginTop="450.0dp"/>

 

        <Button

        android:text=""

        android:layout_width="570.0dp"

        android:layout_height="45.5dp"

        android:id="@+id/button6"

        android:layout_marginTop="400.0dp"/>

 

        <Button

        android:text=""

        android:layout_width="570.0dp"

        android:layout_height="45.5dp"

        android:id="@+id/button7"

        android:layout_marginTop="350.0dp"/>

 

        <Button

        android:text=""

        android:layout_width="570.0dp"

        android:layout_height="45.5dp"

        android:id="@+id/button8"

        android:layout_marginTop="300.0dp"/>

 

 

この座標データは、筆者の機種に適合させたものです

 

 

 

画面確認のため、配置を実行します

 

 

 

画面部品のコードをコピーペーストします

 

 

//////////////////////////////////////////

// ここから下が、最初に実行されます

// ここでは、プログラムの初期設定などを定義します

 

Edt_tbl[0] = (EditText)FindViewById<EditText>(Resource.Id.editText0);

 

//Edt_tbl[0].KeyPress += (object sender, View.KeyEventArgs e) =>

//{

//        e.Handled = false;

//        if (e.Event.Action == KeyEventActions.Down && e.KeyCode == Keycode.Enter)

//        {

//                e.Handled = true;

//        }

//};

 

But_tbl[0] = (Button)FindViewById(Resource.Id.button0);

But_tbl[0].Click += Button0_Click;

 

But_tbl[1] = (Button)FindViewById(Resource.Id.button1);

But_tbl[1].Click += Button1_Click;

 

But_tbl[2] = (Button)FindViewById(Resource.Id.button2);

But_tbl[2].Click += Button2_Click;

 

But_tbl[3] = (Button)FindViewById(Resource.Id.button3);

But_tbl[3].Click += Button3_Click;

 

But_tbl[4] = (Button)FindViewById(Resource.Id.button4);

But_tbl[4].Click += Button4_Click;

 

But_tbl[5] = (Button)FindViewById(Resource.Id.button5);

But_tbl[5].Click += Button5_Click;

 

But_tbl[6] = (Button)FindViewById(Resource.Id.button6);

But_tbl[6].Click += Button6_Click;

 

But_tbl[7] = (Button)FindViewById(Resource.Id.button7);

But_tbl[7].Click += Button7_Click;

 

But_tbl[8] = (Button)FindViewById(Resource.Id.button8);

But_tbl[8].Click += Button8_Click;

 

赤線が引かれた部分は、エラーが発生した部分です

 

 

マウスを乗せると、原因と対策が表示されます

 

 

 

 

 

 

対策を選択すると、エラーが消えます

 

 

そして、コードを下に移動すると、イベント関数が追加されていることが確認できます

 

 

 

エラーをすべて消しました

 

 

 

初期設定の残りのコードをコピーペーストします

 

 

//  音声認識の有無判定

string rec = Android.Content.PM.PackageManager.FeatureMicrophone;

if (rec != "android.hardware.microphone")

{

var alert = new AlertDialog.Builder(Edt_tbl[0].Context);

alert.SetTitle("音声マイクがありません");

alert.SetPositiveButton("OK", (sender, e) => {return;});

alert.Show();

Edt_tbl[0].Text = "音声認識出来ません";

 

}

 

 

Path_files = Path_com + @"/files";

Path_key = Path_files + @"/key";

if (System.IO.Directory.Exists(Path_files) == false) Directory.CreateDirectory(Path_files);

if (System.IO.Directory.Exists(Path_key) == false) Directory.CreateDirectory(Path_key);

 

But_tbl[0].Text = "音声変換";

But_tbl[1].Text = "実行/保存";

But_tbl[3].Text = "本日表示";

But_tbl[8].Text = "GPS表示";

 

today_dsp();

 

 

 

配置を実行し画面を確認します

 

 

 

 

 

残りの関数をすべてコピーペーストします

 

 

private async void edit_txt_sav()

{

 

string wrt_cst;

string fil_path;

 

Sys_cal_get();      //  システム日付取得

fil_path = Get_fil_path(Sys_yy_all, Sys_mm, Sys_dd);

 

// ファイルを開く

System.Text.Encoding.GetEncoding("utf-8");

using (StreamWriter writer = new StreamWriter(fil_path, true))

{

wrt_cst = Sys_yy_all.ToString("D4") + "," + Sys_mm.ToString("D2") + "," + Sys_dd.ToString("D2") + ","

+ Sys_th.ToString("D2") + "," + Sys_tm.ToString("D2") + "," + Sys_ts.ToString("D2") + ","

+ Edt_tbl[0].Text + ",";

 

await get_GPS();

wrt_cst += Gps_cst + ",";

 

writer.WriteLine(wrt_cst); // テキストを書き込む

writer.Flush();             // 書き込んだ内容をフラッシュする

}

Edt_tbl[0].Text = Voice_cst = "";

 

}

 

private void today_dsp()

{

Sys_cal_get();      //  システム日付取得

string fil_path = Get_fil_path(Sys_yy_all, Sys_mm, Sys_dd);

string[] cut_tbl = new string[20];

string lin_cst = "";

int lin_no = 0;

 

if (File.Exists(fil_path) == false) return;

 

Edt_tbl[0].Text = "";

// テキストファイル読み込み

System.Text.Encoding.GetEncoding("utf-8");

using (StreamReader red = new StreamReader(fil_path))

{

while (red.EndOfStream == false)

{

// 0     1   2   3   4   5  6

// yyyy  mm  dd  th  tm  ts msg

lin_cst = red.ReadLine();

cut_tbl = lin_cst.Split(',');

lin_no++;

 

Edt_tbl[0].Text += lin_no.ToString() + "." + cut_tbl[6] + "\r\n";

}

}

 

}

 

private async Task get_GPS()

{

FeatureNotSupportedException damy1;

FeatureNotEnabledException damy2;

PermissionException damy3;

Exception damy4;

 

//View view = (View)sender;

//string msg_cst;

 

// 許可を確認する

if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessFineLocation) == (int)Android.Content.PM.Permission.Granted)

{

// We have permission, let's get current Location

try

{

var request = new GeolocationRequest(GeolocationAccuracy.Medium);

var location = await Geolocation.GetLocationAsync(request);

 

if (location != null)

{

// Set Location

Gps_cst = $"{location.Latitude}_{location.Longitude}_{location.Altitude}";

 

//  入力されているEditBoxの後ろに音声変換した文字を追加する

But_tbl[8].Text = Gps_cst;

}

}

catch (FeatureNotSupportedException fnsEx)

{

damy1 = fnsEx;

// Handle not supported on device exception

But_tbl[8].Text = "Not Supported";

 

//Console.WriteLine("Not Supported");

}

catch (FeatureNotEnabledException fneEx)

{

damy2 = fneEx;

// Handle not enabled on device exception

But_tbl[8].Text = "Not Enabled";

//Console.WriteLine("Not Enabled");

}

catch (PermissionException pEx)

{

damy3 = pEx;

// Handle permission exception

But_tbl[8].Text = "No Permission";

//Console.WriteLine("No Permission");

}

catch (Exception ex)

{

damy4 = ex;

// Unable to get location

But_tbl[8].Text = ex.ToString();

//Console.WriteLine("Grr Error");

}

}

else

{

But_tbl[8].Text = "--- 許可がありません ---";

 

// 許可は与えられていません。必要に応じて、根拠と要求を表示します。

if (ActivityCompat.ShouldShowRequestPermissionRationale(this, Manifest.Permission.AccessFineLocation))

{

// Provide an additional rationale to the user if the permission was not granted

// and the user would benefit from additional context for the use of the permission.

// For example if the user has previously denied the permission.

 

// 許可が与えられなかった場合は、ユーザーに追加の根拠を提供します

// また、ユーザーは、アクセス許可を使用するための追加のコンテキストから利益を得ることができます。

// たとえば、ユーザーが以前にアクセス許可を拒否した場合です。

// 

 

//Log.Info(TAG, "追加のコンテキストを提供するために、現在の場所の許可の根拠を取得します。");

//var requiredPermissions = new System.String[] { Manifest.Permission.AccessFineLocation };

//msg_cst = Resource.String.permission_location_rationale;

//Glo_set.Msg_dsp(view, msg_cst);

}

else

{

//ActivityCompat.RequestPermissions(this, new System.String[] { Manifest.Permission.AccessFineLocation }, REQUEST_LOCATION);

}

}

 

 

}

 

 

 

//  ここを追加すると、音声テキストが取得出来た

protected override void OnActivityResult(int requestCode, Android.App.Result resultVal, Intent data)

{

string msg_cst;

 

if (requestCode == VOICE)

{

if (resultVal == Android.App.Result.Ok)

{

var matches = data.GetStringArrayListExtra(RecognizerIntent.ExtraResults);

if (matches.Count != 0)

{

string textInput = Voice_cst + matches[0];

 

Voice_cst = textInput;

 

//  入力されているEditBoxの後ろに音声変換した文字を追加する

msg_cst = Edt_tbl[0].Text;

if (0 < msg_cst.Length) msg_cst += " " + Voice_cst;

else msg_cst = Voice_cst;

 

Edt_tbl[0].SetText(msg_cst, BufferType.Normal);

 

Voice_cst = ""; // 音声テキスト初期化

 

}

else Voice_cst = "";

}

 

 

base.OnActivityResult(requestCode, resultVal, data);

}

}

 

// ファイルパスの取得

private string Get_fil_path(int prm_yy_all, int prm_mm,int prm_dd)

{

string fil_path = Path_files + @"/note-" + prm_yy_all.ToString("D4")+"-"

+ prm_mm.ToString("D2") +"-"+ prm_dd.ToString("D2")+ ".txt";

return fil_path;

}

 

// カレンダー処理

private void Sys_cal_get()

{

DateTime localDate = DateTime.Now;

 

Sys_yy_all = localDate.Year;

Sys_mm = localDate.Month;

Sys_dd = localDate.Day;

Sys_th = localDate.Hour;

Sys_tm = localDate.Minute;

Sys_ts = localDate.Second;

}

 

////////////////////// ここまでコピーペーストします/////////////////

 

GPS取得のエラーを解消します

 

 

 

 

 

 

引き続き、「ALT+Enter」で対策します

 

 

 

 

 

 

 

エラーが解消されたら、GPS表示ボタンを定義します

 

 

await get_GPS();

Gps_cst_tmp = Gps_cst;

 

 

GPSを有効にするため、設定の追加が必要です

 

 

 

スクロールして、関係がありそうなチェックボックスを設定します

 

 

 

 

 

 

 

ここで配置を実行し確認します

 

配置が成功したら、アプリ権限を設定します

 

 

 

 

 

 

 

 

 

 

 

 

GPSボタンの確認をします

 

 

 

音声変換ボタンを設定します

 

 

// 音声変換

string ed_cst = ""; ;

 

if(Type_name != "音声変換")

{

Type_name = "音声変換";

Edt_tbl[0].Text = "";

Voice_cst = "";

}

 

// 保存しないままテキストが残っていたら保存する

ed_cst = Edt_tbl[0].Text;

if (0 < ed_cst.Length) edit_txt_sav();

 

var voiceIntent = new Intent(RecognizerIntent.ActionRecognizeSpeech);

voiceIntent.PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm);

//Intent intent = voiceIntent.PutExtra(RecognizerIntent.ExtraPrompt, Application.Context.GetString(Resource.String.action_settings));

voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputCompleteSilenceLengthMillis, 1500);

voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputPossiblyCompleteSilenceLengthMillis, 1500);

voiceIntent.PutExtra(RecognizerIntent.ExtraSpeechInputMinimumLengthMillis, 15000);

voiceIntent.PutExtra(RecognizerIntent.ExtraMaxResults, 1);

voiceIntent.PutExtra(RecognizerIntent.ExtraLanguage, Java.Util.Locale.Default);

StartActivityForResult(voiceIntent, VOICE);

 

保存実行ボタンを設定します

 

 

// 保存 実行

//  エディットボックスの取得

edit_txt_sav();

 

本日表示ボタンを設定します

 

 

Type_name = "本日表示";

today_dsp();

 

ここまでを配置し、動作確認します

 

 

 

 

 

 

 

 

最後に、初期設定ループのコメント行を有効に戻します