import java.util.List;
import com.example.texttablelayout.R;
import com.sekorm.common.DialogUtils;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
public class MainActivity extends Activity {
// 相关按钮
private EditText orderNo;
private Button orderNoCheck;
private EditText barcode;
private TableRow tablerow;
private TableLayout table;
private Button orderCheck;
private Button isNext;
// 动态添加相关控件
private EditText seqNo;
private EditText modelNo;
private EditText number;
private EditText state;
private EditText del;
// 相关存储
List
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* 初始化控件
*/
orderNo =(EditText) findViewById(R.id.orderNo);
table = (TableLayout) findViewById(R.id.tablelayout);
orderNoCheck=(Button) findViewById(R.id.orderNoCheck);
barcode = (EditText) findViewById(R.id.barcode);
orderCheck=(Button) findViewById(R.id.orderCheck);
isNext=(Button) findViewById(R.id.isNext);
/**
* 定义扫描事件
*/
barcode.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// 这两个条件必须同时成立,如果仅仅用了enter判断,就会执行两次
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
// 执行发送消息等操作
String number=barcode.getText().toString();
boolean iscreateLineFlag=isCreateNewLine();
if(iscreateLineFlag){
addWegit(number);
}else{
insertValue(number);
}
barcode.setText("");
barcode.setFocusable(true);
barcode.setFocusableInTouchMode(true);
barcode.requestFocus();
barcode.findFocus();
return true;
}
return false;
}
});
/**
* 1.出库号检查前,设置扫描输入框和检查按键为不可编辑状态
*/
barcode.setEnabled(false);
orderCheck.setEnabled(false);
/**
* 1.定义出库号检查事件
*/
orderNoCheck.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String isNextStr=isNext.getText().toString();
if("0".equals(isNextStr)){
checkOrder();
}else{
//清空扫描下一批出库单;
DialogUtils.showOKCancel(MainActivity.this, "系统提示", "请确认是否要清空所有扫描下一批物料!", "确定", "返回", nextOrderOk, nextOrderCancel);
}}
});
}
// 添加控件相关方法
public void addWegit(String tag) {
tablerow = new TableRow(MainActivity.this);
tablerow.setBackgroundColor(Color.WHITE);
// tablerow.set
seqNo = new EditText(MainActivity.this);
modelNo = new EditText(MainActivity.this);
number = new EditText(MainActivity.this);
state = new EditText(MainActivity.this);
del = new EditText(MainActivity.this);
setDelStyle(del);
setStyle(seqNo);
setStyle(modelNo);
setStyle(number);
setStyle(state);
setStyle(del);
number.setInputType(InputType.TYPE_CLASS_NUMBER);
//设置不可编辑
setEnable(seqNo);
setEnable(modelNo);
modelNo.setSingleLine(false);
setEnable(number);
setEnable(state);
modelNo.setText(tag);
state.setText("OK");
state.setTextColor(Color.GREEN);
int lineNo=createLineNo();
seqNo.setText(String.valueOf(lineNo));
del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TableRow tableRow = (TableRow) view.getParent();
EditText edt=(EditText) tableRow.getChildAt(3);
String flag=edt.getText().toString();
EditText delEdt=(EditText) tableRow.getChildAt(4);
if("OK".equals(flag)){
edt.setText("DEL");
edt.setTextColor(Color.RED);
delEdt.setText(R.string.btn_recover);
}else{
edt.setText("OK");
edt.setTextColor(Color.GREEN);
delEdt.setText(R.string.btn_del);
}
}
});
tablerow.addView(seqNo);
tablerow.addView(modelNo);
tablerow.addView(number);
tablerow.addView(state);
tablerow.addView(del);
table.addView(tablerow, 1,new TableLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && resultCode == RESULT_OK) {
//title = data.getExtras().getString("title");
//money = data.getExtras().getString("money");
//num = data.getExtras().getString("num");
}
super.onActivityResult(requestCode, resultCode, data);
}
private void setEnable(EditText edi){
edi.setFocusable(false);
edi.setFocusableInTouchMode(false);
}
/**
* 生成行号
*/
public int createLineNo(){
int lineNo=table.getChildCount();
if(lineNo==1){
return 1;
}else{
return lineNo;
}
}
/**
* 判读是否新增一行
* @return
*/
public boolean isCreateNewLine(){
for ( int i = 0; i < table.getChildCount(); i++) {
if (i != 0) {
TableRow row = (TableRow) table.getChildAt(i);
EditText modelNoEdt = (EditText) row.getChildAt(1);
String model=modelNoEdt.getText().toString();
EditText state = (EditText) row.getChildAt(3);
String stateStr=state.getText().toString();
if("DEL".equals(stateStr)){
continue;
}
if(TextUtils.isEmpty(model)){
return false;
}
EditText numberNoEdt = (EditText) row.getChildAt(2);
String number=numberNoEdt.getText().toString();
if(TextUtils.isEmpty(number)){
return false;
}
}
}
return true;
}
/**
* 填充数据
* @param val
*/
public void insertValue(String val){
for ( int i = 0; i < table.getChildCount(); i++) {
if (i != 0) {
TableRow row = (TableRow) table.getChildAt(i);
EditText modelNoEdt = (EditText) row.getChildAt(1);
String model=modelNoEdt.getText().toString();
if(TextUtils.isEmpty(model)){
modelNoEdt.setText(val);
break;
}
EditText numberNoEdt = (EditText) row.getChildAt(2);
String number=numberNoEdt.getText().toString();
if(TextUtils.isEmpty(number)){
if(val.length()>4){
DialogUtils.ShowMsgDialog(MainActivity.this, "系统提示", val+" 数量大于或等于4位", numberNoEdt, val);
}else{
numberNoEdt.setText(val);
}
break;
}
}
}
}
/**
* 获取光标
* @param edt
*/
public void getFocus(View edt){
edt.setFocusable(true);
edt.setFocusableInTouchMode(true);
edt.requestFocus();
edt.findFocus();
}
/**
* 边框样式
* @param view
*/
public void setStyle(EditText edt){
TableRow.LayoutParams params = new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
edt.setBackgroundResource(R.drawable.edit_bg);
edt.setLayoutParams(params);
edt.setGravity(Gravity.CENTER);
edt.setTextSize(25);
}
public void setDelStyle(EditText del){
del.setClickable(true);
del.setFocusable(false);
del.setGravity(Gravity.CENTER);
del.setText(R.string.btn_del);
}
/**
* 验证出库单号
*/
public void checkOrder(){
String orderNoStr=orderNo.getText().toString();
if(TextUtils.isEmpty(orderNoStr)){
DialogUtils.showOK(MainActivity.this, "系统提示", "请输入出库单号", "确定");
}else{
if(!TextUtils.isDigitsOnly(orderNoStr)){
DialogUtils.showOK(MainActivity.this, "系统提示", "出库单号必须为数字", "确定");
}else{
barcode.setEnabled(true);
orderCheck.setEnabled(true);
getFocus(barcode);
orderNoCheck.setText("清空,扫下一批");
orderNo.setEnabled(false);
isNext.setText("1");
}
}
}
/**
* 扫描下一批确定事件
*/
android.content.DialogInterface.OnClickListener nextOrderOk= new DialogInterface.OnClickListener() {//添加确定按钮
@Override
public void onClick(DialogInterface dialog, int which) {
table.removeViews(1, table.getChildCount()-1);
isNext.setText("0");
orderNoCheck.setText("开始扫描");
orderNo.setText("");
orderNo.setEnabled(true);
barcode.setEnabled(false);
orderCheck.setEnabled(false);
}
};
/**
* 扫描下一批返回事件
*/
android.content.DialogInterface.OnClickListener nextOrderCancel= new DialogInterface.OnClickListener() {//添加确定按钮
@Override
public void onClick(DialogInterface dialog, int which) {
}
};
}