Android开发常用代码块(pref/Intent/Toast/粘贴板/ED焦点/添加ing...)

(一) sharedPreferences

//存
SharedPreferences.Editor editor = getSharedPreferences("bdduserdata",MODE_PRIVATE).edit();
                              editor.putString("name", name);
                              editor.putString("token", token);
                              editor.putString("headUrl", photourl);
                              editor.apply();
//取
SharedPreferences pref = getActivity().getSharedPreferences("bdduserdata", Context.MODE_PRIVATE);
username = pref.getString("name","");
usertoken = pref.getString("token","");
headurl = pref.getString("headUrl","");

(二):吐司

Toast.makeText(LoginActivity.this,"手机或密码错误,请重新输入!",Toast.LENGTH_LONG).show();

(三): 复制粘贴到粘贴版

    //获取系统CLIPBOARD_SERVICE服务 得到copy实例 就搞定了
private class ClickListenerImpl implements OnClickListener {
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //复制  
            case R.id.button:
                ClipboardManager copy = (ClipboardManager) MainActivity.this
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                copy.setText("Dreamsir");
                break;
            //粘贴  
            case R.id.textView:
                ClipboardManager paste = (ClipboardManager) MainActivity.this
                        .getSystemService(Context.CLIPBOARD_SERVICE);
                String content=paste.getText().toString().trim();
                mTextView.setText(content);
                break;

            default:
                break;
        }
    }

}  

(四): 进入界面时EdutText不自动获得焦点

//在EditText的父级控件中找一个,设置成
//这样可以EditText默认的行为截断
android:focusable="true"
android:focusableInTouchMode="true"

(五): EditText焦点靠右 从右边输入

1.在xml中进行属性的设置

android:gravity="right"

2.在activity里,使用代码控制

editText.setGravity(Gravity.RIGHT);

3.EditText setText()之后光标会自动跑到第一个字符之前

editText.setSelection(position);//position为int,指的是光标的位置,设置成EditText输入框中字符的长度,光标则为最后了

5.设置EditText是否有光标

android:cursorVisible="false"            //为false时选中了也没有光标
或editText.setCursorVisible(visible)

(六):获取系统当前时间 ///获取当前时间戳

//获取时间戳
long timeStamp = System.currentTimeMillis();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String nowtime  = formatter.format(curDate);
//大小写区别:一般写为:yyyy-MM-dd HH:mm:ss,其中在小时部分有hh和HH的区别,
//hh为12小时格式,HH为24小时格式。还有最主要的是MM和mm的区别,MM代表的是月份
//只能用在月份上,mm代表的是分钟只能用在分钟上这两个必须固定。其它的位置必须用
//小写的,大写的C#就不认识了。

(七): 获取设备IMEI与序列号

//IMEI(imei)
TelephonyManager tm = (TelephonyManager) ChinaApplication.getAppContext().getSystemService(Context.TELEPHONY_SERVICE);
String imei = tm.getDeviceId();
if(!TextUtils.isEmpty(imei)){
    deviceId.append("imei");
    deviceId.append(imei);
    return deviceId.toString();
}

//序列号(sn)
String sn = tm.getSimSerialNumber();
if(!TextUtils.isEmpty(sn)){
    deviceId.append("sn");
    deviceId.append(sn);
    return deviceId.toString();
}

(八):

fragement传参

Bundle bundle1 = new Bundle();
bundle1.putString(HomePageFatherFragement.CONTENT, "账本");
homePageFatherFragement.setArguments(bundle1);
 currency_symbol = getArguments().getString("which_currency");

(九):

TextView自适应

  app:autoSizeTextType="uniform"
  app:autoSizeMinTextSize="10sp"
  app:autoSizeMaxTextSize="14sp"
  app:autoSizeStepGranularity="0.5sp"

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是 IconShapeOverride.java 的源代码: ```java package com.android.launcher3.graphics; import android.content.Context; import android.content.SharedPreferences; import android.content.res.Resources; import android.graphics.Path; import android.graphics.Rect; import android.os.Build; import android.util.Log; import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; import com.android.launcher3.Utilities; import com.android.launcher3.icons.GraphicsUtils; import com.android.launcher3.icons.IconProvider; import com.android.launcher3.icons.ShapeData; import com.android.launcher3.util.MainThreadInitializedObject; import com.android.launcher3.util.ResourceBasedOverride; /** * Provides custom icon shapes based on user preferences. */ public class IconShapeOverride extends ResourceBasedOverride { public static final String KEY_PREFERENCE = "pref_override_icon_shape"; private static final String TAG = "IconShapeOverride"; private final IconProvider mIconProvider; public IconShapeOverride(Context context) { this(context, IconProvider.INSTANCE); } @VisibleForTesting public IconShapeOverride(Context context, IconProvider iconProvider) { super(context, KEY_PREFERENCE); mIconProvider = iconProvider; } /** * @return the current shape path, or null if not defined. */ @Nullable public ShapeData getShape() { String pathString = getStringValue(); if (pathString == null) { return null; } try { return ShapeData.parse(pathString); } catch (IllegalArgumentException e) { Log.e(TAG, "Unable to parse shape", e); return null; } } /** * @return the current shape path as a {@link Path} instance, or null if not defined. */ @Nullable public Path getShapePath() { ShapeData data = getShape(); return data != null ? data.getPath() : null; } /** * @return the current shape path bounds, or null if not defined. */ @Nullable public Rect getShapeBounds() { ShapeData data = getShape(); return data != null ? data.getBounds() : null; } /** * Returns the shape path for the given context, or null if none is specified. */ public static Path getShapePath(Resources res, SharedPreferences prefs) { IconShapeOverride override = new IconShapeOverride(res); override.setSharedPreferences(prefs); return override.getShapePath(); } /** * Tests whether the current shape is a circle, by checking if all corners of the shape are at * the same distance from the center. */ public boolean isShapeCircle() { Path shape = getShapePath(); if (shape == null) { return false; } Rect bounds = getShapeBounds(); if (bounds == null) { return false; } Rect outBounds = new Rect(); shape.computeBounds(outBounds, true); float centerX = bounds.exactCenterX(); float centerY = bounds.exactCenterY(); float radius = Math.max(centerX - bounds.left, centerY - bounds.top); float maxDeviation = 0; float[] radii = new float[9]; shape.approximate(1f, radii); for (int i = 0; i < radii.length; i += 2) { float deviation = Math.abs(radii[i] - radius); if (deviation > maxDeviation) { maxDeviation = deviation; } } return maxDeviation < GraphicsUtils.EPSILON; } /** * Updates the default icon shape, if the user has not overridden it. */ public static void updateDefaultShape(Context context) { SharedPreferences prefs = Utilities.getPrefs(context); if (prefs.contains(KEY_PREFERENCE)) { return; } IconShapeOverride override = new IconShapeOverride(context); Path path = override.getDefaultShape(); if (path != null) { prefs.edit().putString(KEY_PREFERENCE, ShapeData.toString(path)).apply(); } } /** * @return the default shape path for the current device. */ @Nullable public Path getDefaultShape() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { return IconShape.applyMaskIfNeeded(mIconProvider.getDeviceProfile(), mIconProvider.getIconMask()); } return null; } @Override protected void onValueChanged() { super.onValueChanged(); mIconProvider.clearCaches(); } } ``` 该类提供了自定义应用图标的形状的功能,它会根据用户的偏好设置提供自定义图标形状。其中包含了获取、设置、更新默认图标形状等方法。此外,还包含一些辅助方法,如测试当前形状是否为圆形。在 Android Oreo 及以上版本中,会调用 IconShape 类提供的方法来获取默认图标形状。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

许进进

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值