構成
- Main
- MyHeader
- MyContents
- MyInputPanel
- MyHeader
package com.efolab.MainActivity;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
public class Main extends Activity {
Context context;
LinearLayout baseLayout;
LinearLayout.LayoutParams params;
private final int
FP = ViewGroup.LayoutParams.FILL_PARENT,
WC = ViewGroup.LayoutParams.WRAP_CONTENT;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getApplicationContext();
baseLayout = new LinearLayout(context);
baseLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(baseLayout);
LinearLayout[] _contents = {
new MyHeader(context) ,
new MyContents(context),
new MyInputPanel(context),
new MyFooter(context)
};
int[][] _params = { { FP, WC }, { FP, 0 }, { FP, WC }, { FP, WC } };
float[] _weight = { 0, 1.0f, 0 , 0 };
for (int i = 0; i < _contents.length; i++) {
params = new LinearLayout.LayoutParams(_params[i][0], _params[i][1]);
params.weight = _weight[i];
baseLayout.addView(_contents[i], params);
}
}
public static OnClickListener l = new OnClickListener(){
@Override
public void onClick(View v) {
TODO
Log.v("OnClick",""+v.getTag());
MyContents.changeContentsText(""+v.getTag());
}
};
}
MyHeader.java
package com.efolab.MainActivity;
import android.content.Context;
import android.widget.LinearLayout;
import android.widget.TextView;
@author
public class MyHeader extends LinearLayout{
public MyHeader(Context context) {
super(context);
TODO
TextView tv = new TextView(context);
tv.setText("HEADER");
this.addView(tv);
}
}
MyContents.java
package com.efolab.MainActivity;
import android.content.Context;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyContents extends LinearLayout {
static TextView tv;
public MyContents(Context context) {
super(context);
TODO
tv = new TextView(context);
this.addView(tv);
}
public static void changeContentsText(String str){
tv.setText(str);
}
}
MyInputPanel.java
package com.efolab.MainActivity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import com.efolab.MainActivity.InputPanel.NumberTable;
import com.efolab.MainActivity.InputPanel.SubLinear;
public class MyInputPanel extends LinearLayout {
@SuppressWarnings("unused")
private static final int
WC = ViewGroup.LayoutParams.WRAP_CONTENT,
FP = ViewGroup.LayoutParams.FILL_PARENT;
public MyInputPanel(Context context) {
super(context);
Log.v("height","height="+this.getHeight());
this.setBackgroundColor(Color.GRAY);
TableLayout tableLeft = new NumberTable(context);
LinearLayout.LayoutParams lpLeft = new LinearLayout.LayoutParams(0 , FP);
lpLeft.weight = 0.6f;
this.addView(tableLeft , lpLeft);
LinearLayout linearRight = new SubLinear(context);
LinearLayout.LayoutParams lpRight = new LinearLayout.LayoutParams(0 , FP);
lpRight.weight = 0.4f;
this.addView(linearRight , lpRight);
}
}
NumberTable.java
package com.efolab.MainActivity.InputPanel;
import android.content.Context;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import com.efolab.MainActivity.Main;
public class NumberTable extends TableLayout{
private static final int
WC = ViewGroup.LayoutParams.WRAP_CONTENT,
FP = ViewGroup.LayoutParams.FILL_PARENT;
public NumberTable(Context context) {
super(context);
XXX
Button[] buttons = new Button[9];
final int iMaxRow = 3;
final int iMaxCol = 3;
int index = 0;
for(int iRow=0;iRow<iMaxRow;iRow++){
TableRow row = new TableRow(context);
for(int iCol = 0;iCol<iMaxCol;iCol++){
String value = ""+(index+1) % 10;
buttons[index] = new Button(context);
buttons[index].setText(value);
buttons[index].setTag(value);
buttons[index].setOnClickListener(Main.l);
row.addView(buttons[index]);
TableRow.LayoutParams _params = new TableRow.LayoutParams(0 , FP);
_params.weight = 0.33f;
buttons[index].setLayoutParams(_params);
buttons[index].setGravity(Gravity.CENTER);
index++;
}
this.addView(row);
}
TableRow row_4 = new TableRow(context);
Button btn_0 = new Button(context);
btn_0.setText("0");
btn_0.setTag("0");
row_4.addView(btn_0);
this.addView(row_4);
TableRow.LayoutParams lpRow_4 = new TableRow.LayoutParams(0 , WC);
lpRow_4.weight = 1.0f;
btn_0.setLayoutParams(lpRow_4);
btn_0.setOnClickListener(Main.l);
}
}
SubLinear.java
package com.efolab.MainActivity.InputPanel;
import android.content.Context;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import com.efolab.MainActivity.Main;
public class SubLinear extends LinearLayout{
@SuppressWarnings("unused")
private static final int
WC = ViewGroup.LayoutParams.WRAP_CONTENT,
FP = ViewGroup.LayoutParams.FILL_PARENT;
public SubLinear(Context context) {
super(context);
TODO
LinearLayout.LayoutParams lpLinear;
this.setOrientation(LinearLayout.VERTICAL);
Button btn_cls = new Button(context);
btn_cls.setText("けす");
lpLinear = new LinearLayout.LayoutParams(FP,0);
lpLinear.weight = 0.25f;
this.addView(btn_cls ,lpLinear);
Button btn_ok = new Button(context);
btn_ok.setText("こたえ");
lpLinear = new LinearLayout.LayoutParams(FP,0);
lpLinear.weight = 0.75f;
this.addView(btn_ok, lpLinear);
btn_cls.setTag("clear");
btn_ok.setTag("ok");
btn_cls.setOnClickListener(Main.l);
btn_ok.setOnClickListener(Main.l);
}
}
MyFooter.java
package com.efolab.MainActivity;
import android.content.Context;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyFooter extends LinearLayout {
public MyFooter(Context context) {
super(context);
TODO
TextView tv = new TextView(context);
tv.setText("Footer");
this.addView(tv);
}
}