小编Daw*_*yży的帖子

如何以编程方式从android中的像素计算dp

我想dppx编程上计算.怎么做?我得到的答案是:

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
ht = displaymetrics.heightPixels;
wt = displaymetrics.widthPixels;
Run Code Online (Sandbox Code Playgroud)

android resolution android-layout

120
推荐指数
3
解决办法
9万
查看次数

两次之间的时差

我希望以hh:mm格式显示两次之间的差异.

第一次是来自数据库,第二次是系统时间.时差每秒更新一次.

我怎样才能做到这一点?

目前我正在使用两个手动时间如果这完全有效,那么我将它实现到我的应用程序中.

public class MainActivity extends Activity 
{
    TextView mytext;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Timer updateTimer = new Timer();
        updateTimer.schedule(new TimerTask() 
        {
            public void run() 
            {
                try 
                {
                    TextView txtCurrentTime= (TextView)findViewById(R.id.mytext);
                    SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
                    Date date1 = format.parse("08:00:12 pm");
                    Date date2 = format.parse("05:30:12 pm");
                    long mills = date1.getTime() - date2.getTime();
                    Log.v("Data1", ""+date1.getTime());
                    Log.v("Data2", ""+date2.getTime());
                    int hours = (int) (mills/(1000 * 60 * 60));
                    int mins = (int) (mills % (1000*60*60));

                    String …
Run Code Online (Sandbox Code Playgroud)

time android

21
推荐指数
3
解决办法
6万
查看次数

Checkstyle,无法创建根模块

我正在尝试在项目中配置Checkstyle.我已经添加:

    apply plugin: 'checkstyle'
    checkstyle {
        // assign the latest checkstyle version explicitly
        // default version is very old, likes 5.9
        toolVersion = '8.6'
        // checkstyle.xml copy from:
        // https://raw.githubusercontent.com/checkstyle/checkstyle/checkstyle-8.6/src/main/resources/google_checks.xml
        // the version should be as same as plugin version
        configFile = rootProject.file('config/checkstyle/checkstyle.xml')
    }
    task Checkstyle(type: Checkstyle) {
        source 'src/main/java'
        include '**/*.java'
        exclude '**/gen/**'
        exclude '**/R.java'
        exclude '**/BuildConfig.java'

        // empty classpath
        classpath = rootProject.files()
    }
Run Code Online (Sandbox Code Playgroud)

到我的根gradle文件中allprojects.

但是当我跑步的时候 ./gradlew checkstyle

我明白了:

* What went wrong:
Execution failed for task …
Run Code Online (Sandbox Code Playgroud)

android checkstyle

17
推荐指数
5
解决办法
8286
查看次数

使用String将枚举写入parcel

我有这样的Parcelable枚举:

 public enum Option implements Parcelable {

    DATA_BASE, TRIPS, BIG_PHOTOS,
    OLD_PHOTOS, FILTERS_IMAGES,
    CATEGORIES_IMAGES, PAGES,
    SOUNDS, PUBLIC_TRANSPORT, MAPS;        

    public static final Parcelable.Creator<Option> CREATOR = new Parcelable.Creator<Option>() {

        public Option createFromParcel(Parcel in) {
            return Option.values()[in.readInt()];
        }

        public Option[] newArray(int size) {
            return new Option[size];
        }

    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(ordinal());
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我修改了它,它看起来像:

 public enum Option implements Parcelable {

    DATA_BASE("Database"), TRIPS("Trips"), BIG_PHOTOS("BigPhotos"),
    OLD_PHOTOS("OldPhotos"), FILTERS_IMAGES("FiltersImages"),
    CATEGORIES_IMAGES("CategoriesImages"), PAGES("Pages"),
    SOUNDS("Sounds"), PUBLIC_TRANSPORT("PublicTransport"), MAPS("Maps"); …
Run Code Online (Sandbox Code Playgroud)

java enums android parcelable

11
推荐指数
2
解决办法
1万
查看次数

在ArrayAdaper中,指定为非null的参数为null

我为spinner扩展了ArrayAdapter:

class OrderAdapter(context: Context, resource: Int, objects: List<Order>) : ArrayAdapter<Order>(context, resource, objects) {
        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
            val view = super.getView(position, convertView, parent)
            view?.let { view.find<TextView>(android.R.id.text1).text = getItem(position).name }
            return view
        }
        override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View? {
            val view = super.getDropDownView(position, convertView, parent)
            view?.let {view.find<TextView>(android.R.id.text1).text = getItem(position).name }
            return view
        }
    }
Run Code Online (Sandbox Code Playgroud)

我得到例外:

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter convertView
                                                                                          at com.github.blabla.endlesss.ui.adapter.OrderAdapter.getView(OrderAdapter.kt:0)
Run Code Online (Sandbox Code Playgroud)

任何想法如何解决它?

android kotlin

9
推荐指数
2
解决办法
1万
查看次数

HiltAndroidTest 中的同一个文件有多个活动的 DataStore

我刚刚将 DataStore 添加到我们的代码库中。之后,我发现所有顺序 UI 测试都失败 - 测试用例中的第一个测试通过,但下一个失败并显示There are multiple DataStores active for the same file.

\n

我使用 Hilt 提供一个数据存储实例

\n
@InstallIn(SingletonComponent::class)\n@Module\ninternal object DataStoreModule {\n\n    @Singleton\n    @Provides\n    internal fun provideConfigurationDataStore(\n        @ApplicationContext context: Context,\n        configurationLocalSerializer: ClientConfigurationLocalSerializer\n    ): DataStore<ClientConfigurationLocal> = DataStoreFactory.create(\n        serializer = configurationLocalSerializer,\n        produceFile = { context.dataStoreFile("configuration.pb") }\n    )\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我猜这是因为In a Hilt test, the singleton component\xe2\x80\x99s lifetime is scoped to the lifetime of a test case rather than the lifetime of the Application.

\n

关于如何解决这个问题有什么想法吗?

\n

android datastore android-uiautomator dagger-hilt

8
推荐指数
1
解决办法
3048
查看次数

布局中的算术运算 - Android数据绑定

我正在尝试在数据绑定中使用算术运算:

<Space
    android:layout_width="match_parent"
    android:layout_height="@{2 * @dimen/button_min_height}" />
Run Code Online (Sandbox Code Playgroud)

不幸的是我得到了:

Error:(47, 47) must be able to find a common parent for int and float 
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

android android-layout android-databinding

6
推荐指数
1
解决办法
3292
查看次数

SearchView不会填充整个宽度

我正在通过菜单将SearchView添加到我的工具栏:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_search_white"
        android:orderInCategory="0"
        android:title="@android:string/search_go"
        android:visible="true"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:iconifiedByDefault="false"
        app:showAsAction="always" />
</menu>
Run Code Online (Sandbox Code Playgroud)

我不知道为什么但是SearchView被移到了右边: 在此输入图像描述

任何想法为什么会发生这种情况?

layout android menu searchview

6
推荐指数
1
解决办法
1028
查看次数

setClickable()无法处理按钮

我想使用setClicable()使按钮无法点击但它不起作用.我正在使用inflater,因为我需要.这是我的代码:

mContactList = (LinearLayout) findViewById(R.id.contactList);
LayoutInflater inflater = getLayoutInflater();
for (ListIterator<ContactModel> it = contactList.listIterator(); it.hasNext();){
        ContactModel contact = it.next();

View view = inflater.inflate(R.layout.contact_unknown_list_row, null);
view.findViewById(R.id.inviteButton).setTag(contact.getEmail());
view.findViewById(R.id.inviteButton).setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {                   
        String address = (String) v.getTag();
        sendInvatoin(address);
        if(v.findViewById(R.id.inviteButton).isClickable())
        v.findViewById(R.id.inviteButton).setClickable(false);
    }
    });
mContactList.addView(view);
}
Run Code Online (Sandbox Code Playgroud)

layout android view button

3
推荐指数
1
解决办法
7131
查看次数

如何使用内容提供程序删除第一个"n"行

我想使用内容提供程序删除第一个"n"行.

这是我的代码:

    private void deleteOldItems(int number) {
    String where = HistoryTable.COLUMN_ID + "<=?";
    getContentResolver()
            .delete(Uri.parse("content://com.ipiit.client.contentprovider.HistoryContentProvider/histories"),
                    where, new String[] {String.valueOf(number)});
    }
Run Code Online (Sandbox Code Playgroud)

我工作但只有一次.如何始终删除第一行?

sqlite android android-contentprovider

1
推荐指数
1
解决办法
1127
查看次数