你好,我是Android开发的新手我正在制作一个applock应用程序,我正在使用开关来锁定/解锁应用程序......我在切换时遇到问题。
当我在应用程序列表中“打开”任何开关以正确锁定当前开关中的第11个开关时,它也会“开启”并且一直持续到列表结尾....意味着我只切换一个开关在bt上我发现许多开关打开,当我把任何一个关闭所有开关关闭..
我没有得到我犯错的地方
我的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp" >
<ImageView
android:id="@+id/image"
android:layout_weight="1"
android:layout_width="50dp"
android:layout_height="40dp" />
<TextView
android:id="@+id/text"
android:layout_weight="2"
android:layout_width="80dp"
android:layout_height="60dp"
android:padding="10dp"
android:textSize="16sp" />
<Switch
android:id="@+id/switch1"
android:layout_weight="1"
android:gravity="right"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
这是我的java代码......
public class MainActivity extends ListActivity implements CompoundButton.OnCheckedChangeListener {
Switch s;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
final ArrayList<ResolveInfo> list =(ArrayList<ResolveInfo>) pm.queryIntentActivities(intent,0);
final ArrayAdapter<ResolveInfo> adapter = new ArrayAdapter<ResolveInfo>(this, R.layout.activity_main, list)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main, parent, false);
final String text = list.get(position).activityInfo.applicationInfo.loadLabel(pm).toString();
((TextView)convertView.findViewById(R.id.text)).setText(text);
final Drawable drawable = list.get(position).activityInfo.applicationInfo.loadIcon(pm);
((ImageView)convertView.findViewById(R.id.image)).setImageDrawable(drawable);
s= (Switch) findViewById(R.id.switch1);
return convertView;
}
};
setListAdapter(adapter);
if(s!=null)
{
s.setOnCheckedChangeListener(this);
}
}
@Override
public void onCheckedChanged(CompoundButton button, boolean ischecked) {
Toast.makeText(this, "Monitored switch is " + (ischecked ? "on" : "off"), Toast.LENGTH_SHORT).show();
}
}