我正在寻找一种在用户按下通知中的某个按钮后关闭通知面板的方法。目前,我正在这样做:
sendBroadcast(Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))
Run Code Online (Sandbox Code Playgroud)
但是,这种方式在 Android 12 上已被弃用,官方建议使用GLOBAL_ACTION_DISMISS_NOTIFICATION_SHADE来自AccessibilityService.
有没有办法以某种方式做到这一点而不需要AccessibilityService?
我开始学习Kotlin,我无法理解如何处理异常.我知道没有经过检查的,但无论如何,如果我的方法抛出一个,我该怎么办?例如:
fun test(a: Int, b: Int) : Int {
return a / b
}
Run Code Online (Sandbox Code Playgroud)
如果我除以零,我将有ArithmeticException.那我该怎么办呢?
我需要将图像保存到camera文件夹,但是由于不建议使用Android Q getExternalStoragePublicDirectory,因此我以另一种方式进行操作。我所拥有的(此方法接收位图及其名称):
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver resolver = mContext.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, "DCIM/" + IMAGES_FOLDER_NAME);
Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
OutputStream fos = resolver.openOutputStream(imageUri);
saved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} else {
String imagesDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM).toString() + File.separator + IMAGES_FOLDER_NAME;
File file = new File(imagesDir);
if (!file.exists()) {
file.mkdir();
}
File image = new File(
imagesDir,
name + ".png"
);
final long fileHashCode = image.hashCode();
Logger.d(TAG, "saveImage, …Run Code Online (Sandbox Code Playgroud) I have a timepicker, everything works well but the problem is that I can't make it look like designer wants. For now it looks like this:
I need to hide this keyboard icon under the buttons. How can I do it? It's just a prototype, so here's only its xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:tag="ww">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:amPmBackgroundColor="#6400AA"
android:numbersSelectorColor="#6400AA" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:orientation="horizontal">
<Button
android:id="@+id/button2" …Run Code Online (Sandbox Code Playgroud) 我正在 DCIM 目录中保存图像,但在某些情况下,我需要删除它。以前,我只调用了 image.delete(),其中图像是文件。但现在这张图片以另一种方式保存:
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, "DCIM" + File.separator + IMAGES_FOLDER_NAME);
Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
OutputStream fos = resolver.openOutputStream(imageUri);
boolean saved = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
Run Code Online (Sandbox Code Playgroud)
我尝试使用其名称进行查询并调用 contentResolver.delete(...),但它不起作用。
我有权写入外部存储,但我不想使用 SAF。
我怎样才能删除这样的文件?
我开始在Kotlin中构建应用程序,我想知道如何正确初始化变量。例如在Java中,它就像:
private TextView mSomeTextView;
Run Code Online (Sandbox Code Playgroud)
然后在某些方法中调用findViewById。但是在Kotlin中,我不能只写这样的东西,我需要:
private val textView: TextView = findViewById(R.id.text)
Run Code Online (Sandbox Code Playgroud)
我像往常一样在onCreate下编写它。问题:合适的地方吗?如果没有,我应该在哪里以及如何做?
我发出POST请求以保护服务器,这需要相当长的时间.我想让它更快.有机会这样做吗?它看起来像这样:
private class GetTokenTask extends AsyncTask<Void, Void, String> {
private final String LOG_TAG = MainActivity.class.getSimpleName();
private Call mCallToServer;
private String mAccessToken;
private OkHttpClient mHttpClient;
private JSONObject mResponseJSON;
private Response mResponseFromServer;
@Override
protected String doInBackground(Void... params) {
mHttpClient = new OkHttpClient();
mCallToServer = mHttpClient.newCall(requestToOAuth());
try {
mResponseFromServer = mCallToServer.execute();
mResponseJSON = new JSONObject(mResponseFromServer.body().string());
mAccessToken = mResponseJSON.getString(getString(R.string.access_token_key));
Log.i(LOG_TAG, mAccessToken);
Log.i(LOG_TAG, JWTUtils.decoded(mAccessToken));
} catch (Exception e) {
e.printStackTrace();
}
return mAccessToken;
}
@Override
protected void onPostExecute(String token) {
super.onPostExecute(token);
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个应该覆盖整个屏幕的视图.但问题是它不包括ToolBar.我试过简单的View,我也试过ImageView了setScaleType(ScaleType.FIT_XY).我无法弄清楚该怎么做.
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity">
<View
android:id="@+id/transparent_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="20"
android:background="@color/darkGrey"
android:visibility="gone">
</View>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:title="Your Wi-Fi is online">
<Button
android:id="@+id/btn_pause"
android:layout_width="90dp"
android:layout_height="36dp"
android:layout_margin="17dp"
android:layout_gravity="end"
android:background="@color/white"
android:text="@string/pause"
android:textColor="@color/midPurple"
android:textSize="14sp" />
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
style="@style/AppTabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar_main"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tabs" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud) 如果我创建一个类,我可以传递一个参数:
class Person(name: String) {
}
Run Code Online (Sandbox Code Playgroud)
我也可以写相同的,但是使用val然后我将能够使用属性来获取此值,当我创建一个对象时.
val person = Person("Name")
person.name
Run Code Online (Sandbox Code Playgroud)
问题是:为什么我只需要一个没有val的参数?我应该在哪里,如何以及为什么使用它?
我EditText有一些颜色,但是当我输入一些文本时,它应该会改变。说它是灰色,我开始输入,它是黄色。这EditText也是重点,因此当此活动开始时-我已经可以输入文本了。我如何尝试:
<style name="LoginEnterField">
<item name="android:textColorHint">@color/black</item>
<item name="android:textColor">@color/black</item>
<item name="android:background">@drawable/selector_edittext</item>
<item name="android:textSize">@dimen/general_text_size</item>
<item name="android:paddingLeft">@dimen/activity_inner_horizontal_margin</item>
<item name="android:paddingRight">@dimen/activity_inner_horizontal_margin</item>
</style>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/grey"/>
<corners android:radius="5dp" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/yellow"/>
<corners android:radius="5dp" />
</shape>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/roundbox_active_style"/>
<item android:drawable="@drawable/roundbox_inactive_style" />
</selector>
Run Code Online (Sandbox Code Playgroud)
我认为关键在于此选择器。我的EditText更改颜色,但始终为黄色。仅当输入一些文字时,如何使其变成黄色?我无法进行任何代码更改!这很重要。我不能仅添加或更改xml文件。
编辑:这是不可能只从xml,所以进行代码更改。
我有一个列表项,其中在LinearLayout中包含两个TextView(垂直),一个在另一个之上。较高位的数据不是静态的,我是从网络上获取的,如果数据太长,它将开始填充新行,而较低的视图将消失。因此,我想使第一个视图仅填充一行。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:descendantFocusability="blocksDescendants"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="@dimen/margin_small">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com..view.ContactView
android:id="@+id/contact_icon"
style="@style/ContactListInitialsText"
android:layout_width="@dimen/contact_favorite_icon_size"
android:layout_height="@dimen/contact_favorite_icon_size"
app:request_photo="true"
app:show_contact_presence="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginStart="@dimen/margin_small"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/contact_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="@color/search_result_text_color"
android:textSize="16sp" />
<TextView
android:id="@+id/contact_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="marquee"
android:singleLine="true"
android:textSize="12sp" />
</LinearLayout>
<CheckBox
android:id="@+id/btn_star"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:button="@drawable/btn_star_select"
android:paddingEnd="@dimen/margin_small" />
</LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud) android ×8
kotlin ×3
android-10.0 ×2
android-12 ×1
coding-style ×1
okhttp ×1
post ×1
textview ×1