package com.example.testPackage; import android.net.Uri; import android.os.Bundle; import android.util.DisplayMetrics; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RelativeLayout; import android.widget.Toast; import android.app.Activity; import android.content.Intent; public class MainActivity extends Activity { private static int HORIZON_MAX_COUNT = 3; private static int BUTTON_COUNT = 15; private static int LEFT_SPACE = 5; //边界距离 private static int TOP_SPACE = 5; private static int VERTICAL_SPACE = 5; //控件间距离 private static int HORIZON_SPACE = 5; private static int BUTTON_HEIGHT = 60; private static int BUTTON_START_ID = 3000; private static int EDIT_HEIGHT = 60; private EditText m_EditViewPhoneNum; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm);; RelativeLayout layout = new RelativeLayout(this); Button buttons[] = new Button[BUTTON_COUNT]; String[] m_strNames = {"1","2","3","4","5","6","7","8","9","*","0","#","call","IP","Mail"}; int nLayoutW = 1; int nlayoutH = 1; int nEditWide = dm.widthPixels; int nPerButtonWide = (dm.widthPixels - LEFT_SPACE)/HORIZON_MAX_COUNT - HORIZON_SPACE; m_EditViewPhoneNum = new EditText(this); m_EditViewPhoneNum.setId(2999); RelativeLayout.LayoutParams btPhoneNum = new RelativeLayout.LayoutParams(nEditWide,EDIT_HEIGHT); btPhoneNum.topMargin = 1; btPhoneNum.leftMargin = 1; layout.addView(m_EditViewPhoneNum); for(int i=0;i<BUTTON_COUNT;i++) { buttons[i] = new Button(this); buttons[i].setId(BUTTON_START_ID+i); buttons[i].setText(m_strNames[i]); String strBuf; strBuf = "nlayoutH:" + nlayoutH + "\tnLayoutW:" + nLayoutW; Log.e(strBuf, "layout"); RelativeLayout.LayoutParams btParams = new RelativeLayout.LayoutParams(nPerButtonWide,BUTTON_HEIGHT); btParams.topMargin = TOP_SPACE + EDIT_HEIGHT + VERTICAL_SPACE + ((BUTTON_HEIGHT + VERTICAL_SPACE) * (i/HORIZON_MAX_COUNT)); btParams.leftMargin = LEFT_SPACE + ((nPerButtonWide + HORIZON_SPACE) * (i%HORIZON_MAX_COUNT)); layout.addView(buttons[i],btParams); } this.setContentView(layout); for(int i=0;i<BUTTON_COUNT;i++) { buttons[i].setTag(i); buttons[i].setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View view) { String strBuf; strBuf = "click button:" + view.getId(); Log.v(strBuf, "按键"); GetViewString(view); } }); } } void PrintPressButton(String strData) { int nIndex = m_EditViewPhoneNum.getSelectionStart(); m_EditViewPhoneNum.getText().insert( nIndex, strData).toString(); m_EditViewPhoneNum.setSelection(nIndex + 1); } void GetViewString(View view) { switch(view.getId()) { case 3000:PrintPressButton("1"); break; case 3001:PrintPressButton("2"); break; case 3002:PrintPressButton("3"); break; case 3003:PrintPressButton("4"); break; case 3004:PrintPressButton("5"); break; case 3005:PrintPressButton("6"); break; case 3006:PrintPressButton("7"); break; case 3007:PrintPressButton("8"); break; case 3008:PrintPressButton("9"); break; case 3009:PrintPressButton("*"); break; case 3010:PrintPressButton("0"); break; case 3011:PrintPressButton("#"); break; case 3012: { String strPhoneNum = m_EditViewPhoneNum.getText().toString(); startPhoneCall(strPhoneNum); }break; case 3013: { String strPhoneNum = m_EditViewPhoneNum.getText().toString(); strPhoneNum = "17911" + strPhoneNum; startPhoneCall(strPhoneNum); } break; case 3014:PrintPressButton("Email"); break; } }; private void startPhoneCall(String strPhoneNum) { if(strPhoneNum==null||"".equals(strPhoneNum.trim())) { Toast.makeText(getApplicationContext(), "没有电话号码",strPhoneNum.length()).show(); } else { Toast.makeText(getApplicationContext(), "有电话号码"+strPhoneNum,Toast.LENGTH_SHORT).show(); Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+strPhoneNum)); startActivity(intent); } } }