<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_gravity="bottom" android:background="@color/colorBlue" android:layout_width="match_parent" android:layout_height="100dp" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:id="@+id/one" android:textColor="@color/colorWhite" android:gravity="center" android:textSize="30sp" android:text="@string/one" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <TextView android:id="@+id/two" android:textColor="@color/colorWhite" android:gravity="center" android:textSize="30sp" android:text="@string/two" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <TextView android:id="@+id/three" android:textColor="@color/colorWhite" android:gravity="center" android:textSize="30sp" android:text="@string/three" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <TextView android:id="@+id/four" android:textColor="@color/colorWhite" android:gravity="center" android:textSize="30sp" android:text="@string/four" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <TextView android:id="@+id/five" android:textColor="@color/colorWhite" android:gravity="center" android:textSize="30sp" android:text="@string/five" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:id="@+id/six" android:textColor="@color/colorWhite" android:gravity="center" android:textSize="30sp" android:text="@string/six" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <TextView android:id="@+id/seven" android:textColor="@color/colorWhite" android:gravity="center" android:textSize="30sp" android:text="@string/seven" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <TextView android:id="@+id/eight" android:textColor="@color/colorWhite" android:gravity="center" android:textSize="30sp" android:text="@string/eight" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <TextView android:id="@+id/nine" android:textColor="@color/colorWhite" android:gravity="center" android:textSize="30sp" android:text="@string/nine" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> <TextView android:id="@+id/delete" android:textColor="@color/colorWhite" android:gravity="center" android:textSize="30sp" android:text="@string/delete" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> </LinearLayout> </LinearLayout> </FrameLayout>
package keyboard.develop.keyboardtest; import android.view.View; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; public class KeyBoardListener { EditText editText; private StringBuilder finalText = new StringBuilder(); KeyBoardListener(View view, final EditText editText) { this.editText = editText; TextView one = view.findViewById(R.id.one); TextView two = view.findViewById(R.id.two); TextView three = view.findViewById(R.id.three); TextView four = view.findViewById(R.id.four); final TextView five = view.findViewById(R.id.five); TextView six = view.findViewById(R.id.six); TextView seven = view.findViewById(R.id.seven); TextView eight = view.findViewById(R.id.eight); TextView nine = view.findViewById(R.id.nine); TextView delete = view.findViewById(R.id.delete); final LinearLayout layout = view.findViewById(R.id.keyBoard); one.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int selection = editText.getSelectionEnd(); finalText.insert(selection, "1"); setTextSelection(); } }); two.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int selection = editText.getSelectionEnd(); finalText.insert(selection, "2"); setTextSelection(); } }); three.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int selection = editText.getSelectionEnd(); finalText.insert(selection, "3"); setTextSelection(); } }); four.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int selection = editText.getSelectionEnd(); finalText.insert(selection, "4"); setTextSelection(); } }); five.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int selection = editText.getSelectionEnd(); finalText.insert(selection, "5"); setTextSelection(); } }); six.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int selection = editText.getSelectionEnd(); finalText.insert(selection, "6"); setTextSelection(); } }); seven.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int selection = editText.getSelectionEnd(); finalText.insert(selection, "7"); setTextSelection(); } }); eight.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int selection = editText.getSelectionEnd(); finalText.insert(selection, "8"); setTextSelection(); } }); nine.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int selection = editText.getSelectionEnd(); finalText.insert(selection, "9"); setTextSelection(); } }); delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int selection = editText.getSelectionEnd(); if (finalText.length() > 0) { finalText = stringToBuilder(finalText.substring(0, selection - 1 == -1 ? 0 : selection - 1) + finalText.substring(selection)); editText.setText(finalText); } editText.setSelection(selection - 1 <= 0 ? 0 : selection - 1); } }); editText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { layout.setVisibility(View.VISIBLE); } }); } private StringBuilder stringToBuilder(String s) { return new StringBuilder(s); } private void setTextSelection() { int selection = editText.getSelectionEnd(); editText.setText(finalText); editText.setSelection(selection + 1); } }
int p = () ? 1 : 2; int p = k == 2 ? 7 : 3; // if (k == 2) p = 7; else p = 3;
package keyboard.develop.keyboardtest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.view.WindowManager; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private LinearLayout layout; private ExecutorThread executorThread; @Override protected void onCreate(Bundle savedInstanceState) { getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); // , super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText editText = findViewById(R.id.edit); layout = findViewById(R.id.keyBoard); new KeyBoardListener(layout, editText); executorThread = new ExecutorThread((TextView)findViewById(R.id.textView)); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {} @Override public void afterTextChanged(Editable editable) { if (!editable.toString().equals("")) { executorThread.setK(Integer.parseInt(editText.getText().toString())); executorThread.doWork(); } } }); } @Override public void onBackPressed() { layout.setVisibility(View.GONE); // } }
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="keyboard.develop.keyboardtest.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:textColor="@color/colorPrimary" android:gravity="center" android:textSize="25sp" android:layout_width="match_parent" android:layout_height="50dp" android:text="Write your text here!"/> <EditText android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="50dp"/> </LinearLayout> <LinearLayout // android:visibility="gone" android:id="@+id/keyBoard" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_gravity="bottom"> <include layout="@layout/keyboard"/> // , </LinearLayout> </FrameLayout>
package keyboard.develop.keyboardtest; import android.widget.TextView; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ExecutorThread { private ExecutorService executorService; private Thread thread; private int p = 0; private int pow = 0; private int k = 0; private TextView textView; ExecutorThread(final TextView text) { p = 0; textView = text; thread = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < pow; i++) p++; String s = "Result " + k + "^4 = " + String.valueOf(p); textView.setText(s); } }); thread.start(); executorService = Executors.newSingleThreadExecutor(); } void doWork() { executorService.submit(thread); } void setK(int k) { p = 0; this.k = k; pow = (int) Math.pow(k, 4); textView.setText("Please wait. We are calcing!"); } }
<TextView android:gravity="center" android:textColor="@color/colorPrimary" android:textSize="25sp" android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="50dp"/>
executorThread = new ExecutorThread((TextView)findViewById(R.id.textView)); editText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {} @Override public void afterTextChanged(Editable editable) { if (!editable.toString().equals("")) { executorThread.setK(Integer.parseInt(editText.getText().toString())); executorThread.doWork(); } } });
executorService.submit(thread);
Source: https://habr.com/ru/post/426223/
All Articles