package com.example.dbaum.ys.yourspeak;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by DBaum on 19.03.2016.
*/
public class Tastatur {
private Context ctx;
private Handler handler;
private Display display;
public Tastatur(Context ctx) {
this.ctx = ctx;
OnClick(); // ertellt den Handler für das Klick event
display = new Display(); // ertellt mein Display für den Taschenrechner
}
private void OnClick()
{
handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
Bundle b = msg.getData();
String T = b.getString("TEXT");
display.setTextViewTxt(display.getSelect(),T);
return true;
}
});
}
private class Display {
private LinearLayout Container;
private ArrayList<TextViewController> TextViewList;
private int select;
public Display()
{
TextViewList = new ArrayList<>();
select = 0;
}
public RelativeLayout getDisplay() {
RelativeLayout relativeLayout = new RelativeLayout(ctx); // Das RelativeLayout ist dafür, das meine Mathematische Aufgabe Zentiert ist
relativeLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, 1));
//Container ist das Layout in dem Meine Ganzen View gelagert werden
/* BSP Buch --- = Buchstrich View
********************************************** <= Lin/ Horizontal
* ************************ <= Lin/Vertikal *
* * ******************** * *
* * * TextView1 * * *
* * ******************** * *******************
* * ------------------- * * TextView3 *
* * ******************** * *******************
* * * TextView2 * * *
* * ******************** * *
* ************************ *
**********************************************
*/
Container = new LinearLayout(ctx);
Container.setOrientation(LinearLayout.VERTICAL);
RelativeLayout.LayoutParams relParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.CENTER_HORIZONTAL | RelativeLayout.CENTER_VERTICAL);
Container.setLayoutParams(relParams);
Container.setGravity(Gravity.CENTER_VERTICAL);
Container.addView(getTextView());
relativeLayout.addView(Container);
return relativeLayout;
}
//Layout
public TextView getTextView() {
TextViewController textViewController = new TextViewController();
TextViewList.add(textViewController);
select = TextViewList.size() - 1; //select das Neue(Letzt)
return textViewController.Tv;
}
public void setTextViewTxt(int Select, String Text) {
TextViewList.get(Select).setText(Text);
}
public int getSelect() {
return select;
}
}
private class KeyBoard implements View.OnClickListener {
private LinearLayout getKey()
{
LinearLayout ll = new LinearLayout(ctx);
ll.setOrientation(LinearLayout.HORIZONTAL);
for(int i = 0;i<=9;i+=1)
{
Button P = new Button(ctx);
P.setText(Integer.toString(i));
P.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
P.setOnClickListener(this);
ll.addView(P);
}
return ll;
}
@Override
public void onClick(View v) {
Bundle b = new Bundle();
b.putString("TEXT", ((TextView) v).getText().toString());
Message p = new Message();
p.setData(b);
handler.sendMessage(p);
}
}
private class TextViewController
{
private String Text;
private TextView Tv;
public TextViewController()
{
Tv = new TextView(ctx, null, R.style.TextLay);
Text = "";
}
public void setText(String text){
int select = Tv.getSelectionEnd()-1;
Tv.append(text, select, select+1) ; //Texteinfügen an der Courser Poition
}
public String getText() {
return Text;
}
}
public LinearLayout getView()
{
LinearLayout LL = new LinearLayout(ctx);
LL.setOrientation(LinearLayout.VERTICAL);
LL.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
LL.addView(display.getDisplay());
LL.addView(new KeyBoard().getKey());
return LL;
}
}
Alles anzeigen