Skip to content

Commit

Permalink
Counter_v1.4
Browse files Browse the repository at this point in the history
  • Loading branch information
LaIixia committed Jun 14, 2024
1 parent 4225d07 commit 8528b55
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 119 deletions.
15 changes: 1 addition & 14 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {
minSdk 21
targetSdk 34
versionCode 1
versionName "1.3"
versionName "1.4"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand All @@ -22,17 +22,18 @@ android {
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.6.0'
implementation 'com.google.android.material:material:1.12.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.+'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

}
28 changes: 16 additions & 12 deletions app/src/main/java/com/myapp/textcounter/Buttons.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,37 @@

import android.os.Build;
import android.text.Editable;
public class Buttons {
import androidx.appcompat.app.AppCompatActivity;

public class Buttons extends AppCompatActivity {
//テキストのカウント
//edittextの文字列の長さを取得するメソッド
public String Count(Editable text){
return Integer.toString (text.length());
public String Count(Editable text) {
return Integer.toString (text.length ( ));
}

//edittextの文字列の長さが0であるか判断するメソッド
public boolean Judge(Editable s){
int L = s.toString ().length();
if(L == 0){
public boolean Judge(Editable s) {
int L = s.toString ( ).length ( );
if (L == 0) {
return true;
}
else {
} else {
return false;
}
}

//Int型からstring型に変換するメソッド
public String format(int lines){
public String format(int lines) {
return Integer.toString (lines);
}

//OS Version Check
public boolean ChkOS(int ver){
if(Build.VERSION.SDK_INT<=ver ){
public boolean ChkOS(int ver) {
if (Build.VERSION.SDK_INT <= ver) {
return true;
}else{
} else {
return false;
}
}
}

103 changes: 56 additions & 47 deletions app/src/main/java/com/myapp/textcounter/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,39 @@
import android.content.ClipboardManager;
import android.content.ClipData;
import android.content.Context;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import android.text.TextWatcher;
import android.text.Editable;
import android.view.View;
import android.os.Bundle;
import android.text.Editable;
import java.util.Objects;
import static androidx.appcompat.app.AppCompatDelegate.*;

public class MainActivity extends AppCompatActivity {//継承
//フィールド
private TextView textLetter,textLines;
private EditText editText;
Buttons bt = new Buttons();

//フィールドonCreate内で呼び出すために、privateで宣言
private Buttons bt ;
private EditText e;
private TextView textLetter ,textLines;
private ClipboardManager clipboard;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//使用アイテムの定義
editText = findViewById(R.id.edit_text);
textLetter = findViewById(R.id.text_letters2);
textLines = findViewById(R.id.text_lines2);
//Activity起動時リソースのidに呼び出して、フィールドに代入
bt = new Buttons();
e = findViewById(R.id.edit_text);
textLetter = findViewById (R.id.text_letters2);
textLines = findViewById (R.id.text_lines2);
clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

//テキストカウント
Button button_cnt = findViewById(R.id.button_cnt);
Expand All @@ -45,23 +51,17 @@ protected void onCreate(Bundle savedInstanceState) {
Button button_pst = findViewById(R.id.button_pst);
button_pst.setOnClickListener(new ButtonPaste());

//スイッチの イベントを作成
//チェックボックス(TextWachar)
CheckBox CB = findViewById(R.id.checkBox);
CB.setOnCheckedChangeListener (new Check());

//スイッチ(テーマ変更) Activity開始時にクラスを呼ぶ
//Switch toggleSwitch = findViewById(R.id.TG_switch);
ToggleButton toggleSwitch = findViewById(R.id.TG_switch);
toggleSwitch.setOnCheckedChangeListener(new onCheckedChangeListener());

//起動時にシステムのダークモードがYESの場合はtrue(ダークモードオン)
if(getDefaultNightMode() == MODE_NIGHT_YES||getDefaultNightMode()==MODE_NIGHT_AUTO_BATTERY){
toggleSwitch.setChecked(true);
}
//そうでない場合はfalse(無効)
else if(getDefaultNightMode()== MODE_NIGHT_NO){
toggleSwitch.setChecked(false);
}
toggleSwitch.setOnCheckedChangeListener(new Theme());
}

// トグルスイッチを押した時に処理するクラス。
class onCheckedChangeListener implements CompoundButton.OnCheckedChangeListener{
static class Theme implements CompoundButton.OnCheckedChangeListener{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
setDefaultNightMode(MODE_NIGHT_YES);
Expand All @@ -71,47 +71,59 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
}
}
class ButtonCount implements View.OnClickListener {
EditText e = editText;

/*
TextEvent型のTWにTextEventクラスのobj生成させることで、
TWでaddイベントを呼ぶと、removeする際に同じ場所(TW)を参照することでwatcherが切れる
*/
class Check implements CompoundButton.OnCheckedChangeListener{
TextEvent TW = new TextEvent ();
public void onCheckedChanged(CompoundButton buttonView1, boolean isChecked) {
View (bt.Count (e.getText ( )), bt.format (e.getLineCount ( )));
if (isChecked) {
e.addTextChangedListener (TW);
}else{
e.removeTextChangedListener (TW);
}
}
}
class TextEvent implements TextWatcher {
public void beforeTextChanged(CharSequence charSequence, int A, int B, int C) {}
public void onTextChanged(CharSequence charSequence, int A, int B, int C) {}
public void afterTextChanged(Editable editable) {
View (bt.Count (e.getText ( )), bt.format (e.getLineCount ( )));
}
}

class ButtonCount implements View.OnClickListener {
public void onClick(View view) {
if(bt.Judge(e.getText())){toast();}
View(bt.Count(e.getText()),bt.format(e.getLineCount()));
if (bt.Judge (e.getText ( ))) {toast ( R.string.noText);}
View (bt.Count (e.getText ( )), bt.format (e.getLineCount ( )));
}
}

class ButtonDelete implements View.OnClickListener {
EditText e = editText;
public void onClick(View view){
e.getText().clear();
View(bt.Count(e.getText()),bt.format(e.getLineCount()));
}
}

class ButtonCopy implements View.OnClickListener {
//クリップボードマネージャー
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
Editable e = editText.getText();
public void onClick(View view) {
if(bt.Judge(e)){toast();}
if(bt.Judge(e.getText ())){toast(R.string.noText);}
else{
// Editのテキストを取得
ClipData clip = ClipData.newPlainText(null, e);
//クリップボードにセット
clipboard.setPrimaryClip(clip);
ClipData clip = ClipData.newPlainText(null, e.getText ());
clipboard.setPrimaryClip(clip);//クリップボードにセット
}
//OS 12L以下かつ、edittext内の文字の長さが0でない時だけバブルを出す
if(bt.ChkOS(32) && e.length() !=0) {
Toast.makeText(getApplicationContext(), R.string.copy, Toast.LENGTH_SHORT).show();
}
if(bt.ChkOS(32) && e.length() !=0) {toast(R.string.copy);}
}
}

class ButtonPaste implements View.OnClickListener {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
EditText e = editText;
public void onClick(View view) {
try {
//グリップボートのデータの位置 0番目
try {//グリップボートのデータの位置 0番目
ClipData.Item item = Objects.requireNonNull (clipboard.getPrimaryClip ( )).getItemAt (0);
String pasteData = item.getText().toString();
e.setText (pasteData);
Expand All @@ -120,9 +132,6 @@ public void onClick(View view) {
catch (Exception e) {System.out.println(e); return;}
}
}
void toast(){Toast.makeText(getApplicationContext(), R.string.noText, Toast.LENGTH_SHORT).show();}
void View(String a,String b){
textLetter.setText (a);
textLines.setText(b);
}
void toast(int id){Toast.makeText(getApplicationContext(), id, Toast.LENGTH_SHORT).show();}
void View(String a,String b){textLetter.setText (a);textLines.setText(b);}
}
34 changes: 27 additions & 7 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/teal"
android:foregroundTint="@color/white"
android:background="@color/BackgroundColor"
android:textAlignment="center"
tools:context=".MyActivity">

Expand All @@ -16,7 +15,7 @@
android:layout_marginStart="80dp"
android:layout_marginBottom="95dp"
android:text="@string/button_cnt"
app:backgroundTint="@color/purple"
android:textColor="@color/TextColorWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />

Expand All @@ -27,7 +26,7 @@
android:layout_marginStart="80dp"
android:layout_marginBottom="45dp"
android:text="@string/button_cpy"
app:backgroundTint="@color/purple"
android:textColor="@color/TextColorWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />

Expand All @@ -37,8 +36,8 @@
android:layout_height="wrap_content"
android:layout_marginEnd="80dp"
android:layout_marginBottom="45dp"
android:backgroundTint="@color/purple"
android:text="@string/button_pst"
android:textColor="@color/TextColorWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

Expand All @@ -48,8 +47,8 @@
android:layout_height="wrap_content"
android:layout_marginEnd="80dp"
android:layout_marginBottom="95dp"
android:backgroundTint="@color/purple"
android:text="@string/button_del"
android:textColor="@color/TextColorWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

Expand All @@ -61,6 +60,7 @@
android:layout_marginTop="45dp"
android:gravity="right"
android:text="@string/letters"
android:textColor="@color/TextColorGray"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Expand All @@ -73,6 +73,7 @@
android:layout_marginTop="80dp"
android:gravity="right"
android:text="@string/lines"
android:textColor="@color/TextColorGray"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Expand All @@ -85,6 +86,7 @@
android:layout_marginEnd="100dp"
android:gravity="right"
android:text="@string/letters_zero"
android:textColor="@color/TextColorGray"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Expand All @@ -97,13 +99,14 @@
android:layout_marginEnd="100dp"
android:gravity="right"
android:text="@string/lines_zero"
android:textColor="@color/TextColorGray"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/edit_text"
android:layout_width="300dp"
android:layout_width="305dp"
android:layout_height="320dp"
android:layout_marginBottom="5dp"
android:autofillHints="@string/hint"
Expand All @@ -121,12 +124,29 @@
android:id="@+id/TG_switch"
android:layout_width="65dp"
android:layout_height="40dp"
android:textColor="@color/ToggleColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.984"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.008" />

<CheckBox
android:id="@+id/checkBox"
android:layout_width="275dp"
android:layout_height="40dp"
android:gravity="center"
android:text="@string/button_chk"
android:textColor="@color/TextColorGray"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.555"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.990" />

<!--
<Switch
android:id="@+id/TG_switch"
Expand Down
Loading

0 comments on commit 8528b55

Please sign in to comment.