首页 > 代码库 > 点击事件中实现弹出一个选择框(如选择网络设置、选择电话短信联系方式)

点击事件中实现弹出一个选择框(如选择网络设置、选择电话短信联系方式)

1、网络设置

public void checkNetwork(){

//获取连接的管理对象
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

//获取当前正在使用的网络
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

//判断网络是否可用
if (networkInfo!=null && networkInfo.isConnected()) {
//网络已连接
if(networkInfo.getType()==ConnectivityManager.TYPE_MOBILE){
Toast.makeText(MainActivity.this, "正在使用手机流量。", Toast.LENGTH_LONG).show();
}else if (networkInfo.getType()==ConnectivityManager.TYPE_WIFI) {
Toast.makeText(MainActivity.this, "正在使用无线网。", Toast.LENGTH_LONG).show();

}

} else {
//网络没有连接的情况
new AlertDialog.Builder(this).setTitle("请设置网络连接").setMessage("网络没有打开,请进行设置")
.setIcon(R.drawable.ic_launcher).setPositiveButton("设置", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "设置", Toast.LENGTH_LONG).show();
//创建网络设置
Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
//执行意图
startActivity(intent);
}
}).setNegativeButton("取消", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_LONG).show();

}
}).create().show();;


}

}

2、选择电话短信联系方式

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
//怎么获取点击我的数据 
Users entity = (Users) parent.getItemAtPosition(position);
//
// Toast.makeText(this, "点击我----"+entity.toString(), Toast.LENGTH_LONG).show();
//
final TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone);
// Toast.makeText(this, "点击我----"+tv_id.getText(), Toast.LENGTH_LONG).show();

//弹出框
new AlertDialog.Builder(this).setTitle("联系此人").setMessage("选择联系方式")
.setIcon(R.drawable.ic_launcher).setPositiveButton("电话", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "电话"+tv_phone.getText(), Toast.LENGTH_LONG).show();
//创建
Intent intent =new Intent(Intent.ACTION_DIAL);
//intent.setData(Uri.parse("tv_phone.getText()"));
//执行意图
startActivity(intent);
}
}).setNegativeButton("短信", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "短信"+tv_phone.getText(), Toast.LENGTH_LONG).show();
//创建
Intent intent =new Intent(Intent.ACTION_VIEW);
//intent.setData(Uri.parse("tel:13713713789"));
intent.setType("vnd.android-dir/mms-sms");
//此为号码
//intent.setData(Uri.parse("content://mms-sms/conversations/"));
intent.putExtra("sms_body", "The SMS text");
//执行意图
startActivity(intent);

}
}).create().show();
}