一、关闭软件盘
public void closeInput() {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null && HelpActivity.this.getCurrentFocus() != null) {
inputMethodManager.hideSoftInputFromWindow(HelpActivity.this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
}二、检测版本号
android.os.Build.VERSION.SDK_INT < 18三、是否支持蓝牙
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
if (mBluetoothAdapter == null) {
}四、是否有连接网络
ConnectivityManager cwjManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cwjManager.getActiveNetworkInfo();
if (info != null && info.isAvailable()) {
return true;
}
else {
return false;
}五、小线程
new Thread(new Runnable() {
@Override
public void run() {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null && PkiHardWareActivity.this.getCurrentFocus() != null) {
inputMethodManager.hideSoftInputFromWindow(PkiHardWareActivity.this.getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}).start();六、textview 文本复制
// TODO Auto-generated method stub
ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
final String temp = textView1.getText().toString();
cmb.setText(temp.toString());
Toast.makeText(context, "Copy success", Toast.LENGTH_SHORT).show();
Vibrator vibrator;
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
long[] pattern = { 100, 100, 0, 0 };
vibrator.vibrate(pattern, -1);
return false;
七、动态布局
tv_left.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
RelativeLayout.LayoutParams lp = (LayoutParams) tv_left.getLayoutParams();
lp.addRule(CENTER_VERTICAL);
tv_left.setLayoutParams(lp);
本文提供了一系列Android开发中常用的代码片段,包括关闭软键盘、检查网络连接状态、版本号检测、蓝牙支持检测等实用功能实现方式,并展示了如何进行文本复制及简单的动态布局调整。
1527

被折叠的 条评论
为什么被折叠?



