当前位置: 编程技术>移动开发
本页文章导读:
▪代码起动插入一个联系人的电话以及打开一个联系人并且适应不同版本 代码启动插入一个联系人的电话以及打开一个联系人并且适应不同版本
public void onClick(View v) {
EditText fld=(EditText)findViewById(R.id.name);
String name=fld.getText().toString();
fld=(EditText)findViewById(R.........
▪ TabActivity 运用 TabActivity 使用
TabActivity
[功能]
一个TabActivity 包含 数个标签
[代码 步骤]
1. 得到 TabHost 的实例
TabHost host = this.getTabHost();
2. 定义 CustomTabs 用于填充 TabHost 各个标签布局用
写道
其.........
▪ 关于socket的一些货色 关于socket的一些东西
自己一直没有涉足这个东西只是有人问
把相关的东西发在这里 以备将来自己用
Use the java.net classes
String cmd("my command");
try {
InetSocketAddress address = new InetSocketAddre.........
[1]代码起动插入一个联系人的电话以及打开一个联系人并且适应不同版本
来源: 互联网 发布时间: 2014-02-18
代码启动插入一个联系人的电话以及打开一个联系人并且适应不同版本
public void onClick(View v) { EditText fld=(EditText)findViewById(R.id.name); String name=fld.getText().toString(); fld=(EditText)findViewById(R.id.phone); String phone=fld.getText().toString(); Intent i=new Intent(Intent.ACTION_INSERT_OR_EDIT); i.setType(Contacts.CONTENT_ITEM_TYPE); i.putExtra(Insert.NAME, name); i.putExtra(Insert.PHONE, phone); startActivity(i); }
加入包名:
import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.Intents.Insert;
2.判断不同的版本
static { int sdk=new Integer(Build.VERSION.SDK).intValue(); if (sdk>=5) { try { Class clazz=Class.forName("android.provider.ContactsContract$Contacts"); CONTENT_URI=(Uri)clazz.getField("CONTENT_URI").get(clazz); // Log.i("clazz",""+clazz); // Log.i("clazz.getField",""+clazz.getField("CONTENT_URI")); // Log.i("CONTENT_URI",""+CONTENT_URI); } catch (Throwable t) { Log.e("PickDemo", "Exception when determining CONTENT_URI", t); } } else { CONTENT_URI=Contacts.People.CONTENT_URI; } }
下面是发起一个供选择的联系人
@Override public void onCreate(Bundle icicle) { super.onCreate(icicle); if (CONTENT_URI==null) { Toast .makeText(this, "We are experiencing technical difficulties...",Toast.LENGTH_LONG) .show(); finish(); return; } setContentView(R.layout.main); Button btn=(Button)findViewById(R.id.pick); btn.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { Intent i=new Intent(Intent.ACTION_PICK, CONTENT_URI); // Log.i("CONTENT_URI",""+CONTENT_URI); startActivityForResult(i, PICK_REQUEST); } }); } @Override protected void onActivityResult(int requestCode, int resultCode,Intent data) { if (requestCode==PICK_REQUEST) { if (resultCode==RESULT_OK) { startActivity(new Intent(Intent.ACTION_VIEW,data.getData())); Log.i("data.getData()",""+data.getData()); } } }
[2] TabActivity 运用
来源: 互联网 发布时间: 2014-02-18
TabActivity 使用
TabActivity
[功能]
一个TabActivity 包含 数个标签
[代码 步骤]
1. 得到 TabHost 的实例
TabHost host = this.getTabHost();
2. 定义 CustomTabs 用于填充 TabHost 各个标签布局用
写道
其必须包含 public View createTabContent(String tag) 定义
public class CustomTabs implements TabHost.TabContentFactory { Activity activity; LayoutInflater inflater; LinearLayout layout; public CustomTabs (Activity a) { activity = a; inflater = activity.getLayoutInflater(); } /** {@inheritDoc} */ public View createTabContent(String tag) { activity.setTitle(tag); return addCustomView(tag); } public View addCustomView(String id){ layout = new LinearLayout(activity); layout.setOrientation(LinearLayout.VERTICAL); if(id.equals(TAB1)){ ImageView iv = new ImageView(activity); iv.setImageResource(R.drawable.o); layout.addView(iv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); } else if(id.equals(TAB2)){ EditText edit = new EditText(activity); layout.addView(edit, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); Button btn = new Button(activity); btn.setText("OK"); layout.addView(btn, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); RadioGroup group = new RadioGroup(activity); group.setOrientation(LinearLayout.HORIZONTAL); RadioButton radio1 = new RadioButton(activity); radio1.setText("Radio 1"); group.addView(radio1); RadioButton radio2 = new RadioButton(activity); radio2.setText("Radio 2"); group.addView(radio2); layout.addView(group, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); } else if(id.equals(TAB3)){ LinearLayout.LayoutParams param3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); layout.addView(inflater.inflate(R.layout.item_3, null),param3); } else if(id.equals(TAB4)){ LinearLayout.LayoutParams param4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); layout.addView(inflater.inflate(R.layout.item_4, null),param4); } else { TextView tv5 = new TextView(activity); tv5.setText("no resource aviable!"); tv5.setGravity(1); layout.addView(tv5, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); } return layout; } }
3. 使用 CustomTabs
host.addTab(host.newTabSpec(TAB1) .setIndicator("Tab 1", getResources().getDrawable(R.drawable.beijing_001_mb5ucom)) .setContent(item));
4. 标签变化的监听函数
host.setOnTabChangedListener(new OnTabChangeListener(){ @Override public void onTabChanged(String tabId) { // TODO Auto-generated method stub } });
5. emulator 运行截图:
* 标签1
* 标签2
* 标签3
1 楼
xianming90
2010-11-06
谢谢,最近在学android,这个实例不错
2 楼
vs_diy
2010-11-07
谢谢。我看看
[3] 关于socket的一些货色
来源: 互联网 发布时间: 2014-02-18
关于socket的一些东西
自己一直没有涉足这个东西只是有人问
把相关的东西发在这里 以备将来自己用
Use the java.net classes
String cmd("my command"); try { InetSocketAddress address = new InetSocketAddress("10.1.1.1", 12350); DatagramPacket request = new DatagramPacket(cmd.getBytes(), cmd.length(), address); DatagramSocket socket = new DatagramSocket(); socket.send(request); } catch (SocketException e) { ... } } catch (IOException e) { ... } }
其他例子
here
using TCP sockets in Java. 和using datagram sockets in Java.
最新技术文章: