0

我正在开发一个锁定特定应用程序的 AppLock。为此,我有一个包含 ListView 的屏幕,并且 ListView 有多个开关,每个安装的应用程序一个。

我需要知道用户打开了哪些开关并提取其中写入的文本。

SensitiveApps.java

package com.sr.droidlock;

import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Switch;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SensitiveApps extends ActionBarActivity {

    Switch appName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensitive_apps);
        final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        final List pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);
        ArrayList<String> tasks = new ArrayList<String>();
        appName = (Switch) findViewById(R.id.appName) ;

        for (Object object : pkgAppsList)
        {
            ResolveInfo info = (ResolveInfo) object;
            final String title  = (String)((info != null) ? getBaseContext().getPackageManager().getApplicationLabel(info.activityInfo.applicationInfo) : "???");
            tasks.add(title);
        }
        Collections.sort(tasks, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                return s1.compareToIgnoreCase(s2);
            }
        });
        ArrayAdapter<String> adapter = new ArrayAdapter <String>(this,R.layout.layout_sensitive_apps, R.id.appName ,tasks);
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
    }
}

activity_sensitive_apps.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.sr.droidlock.SensitiveApps">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView" />
</LinearLayout>

layout_sensitive_apps

<?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" >
    <Switch
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:text="New Switch"
        android:id="@+id/appName" />

</LinearLayout>
4

2 回答 2

2

扩展 ArrayAdapter 并在您需要扩充自己的布局的地方覆盖 getView 方法。因此,它将包含您需要的尽可能多的开关。然后每个开关都可以实现自己的 OnCheckedChangeListener,这样您就可以准确地知道哪个开关被更改了。

编辑

创建一个扩展 ArrayAdapter 并覆盖 getView 的新类 CustomArrayAdapter。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Inflate view
    LayoutInflater inflator = mContext.getLayoutInflater();
    View view = inflator.inflate(R.layout.list_item_topic, null);

    switch = (Switch)view.findViewById(R.id.any_switch);
    switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub

        }
    });

    // TODO bind other views or anything else you need
    return view;
}
于 2016-03-14T13:05:48.330 回答
1

getter我认为如果你在你的模型中创建和setter切换你可以实现你的目标。对于您安装的应用程序(在用户单击时),您设置了一些值,例如setInstalledApp(true);当您循环遍历数组时,请使用开关检查getter单击了哪个应用程序用户。例如:

if(switch.isInstalledApp()){
    //get item data
}else{

} 

希望有所帮助!

于 2016-03-14T13:03:02.400 回答