Android 单击这些复选框时,如何在另一个活动中显示多个复选框信息?

Android 单击这些复选框时,如何在另一个活动中显示多个复选框信息?,android,checkbox,sharedpreferences,Android,Checkbox,Sharedpreferences,我们想要做的是将甜点菜单中选中复选框的信息显示到最终订单(LOFO)活动列表中 这是我们的desserts.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_p

我们想要做的是将甜点菜单中选中复选框的信息显示到最终订单(LOFO)活动列表中

这是我们的desserts.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#ffff"
tools:context=".MainMenu" >


<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="@string/dessertsmenu"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginRight="27dp"
    android:text="@string/meal_ok"
    android:onClick="onClicdesserts_ok" />

<CheckBox
    android:id="@+id/chkpeach"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/chkpie"
    android:layout_below="@+id/chkpie"
    android:defaultValue="false"
    android:text="@string/peachcrumbbars"
    android:onClick="onClickpeachcrumbbars_ok"  />

<CheckBox
    android:id="@+id/chksquares"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/chkpeach"
    android:layout_below="@+id/chkpeach"
    android:defaultValue="false"
    android:text="@string/snickerysquares"
    android:onClick="onClicksnicjerysquares_ok"  />

<CheckBox
    android:id="@+id/chkpie"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="22dp"
    android:layout_marginTop="18dp"
    android:defaultValue="false"
    android:text="@string/blackberrypiebars"
    android:onClick="onClickblackberrypiebars_ok"  />
这是我们要放置已单击复选框信息的地方,即最终订单活动列表

以下是我们的lofo.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#277c9b"
tools:context=".MainMenu" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_below="@+id/textView3"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView4"
    android:layout_below="@+id/textView4"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView5"
    android:layout_below="@+id/textView5"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />
现在的问题是,一旦我们运行它,然后选中其中一个复选框,就会发生错误(找不到源)

非常感谢您的帮助。:)

两件事:

  • 您有:if(chkpie.isChecked());看到结尾的分号了吗?这意味着if语句是无用的
  • 您还有两个文件菜单声明。那是草率和糟糕的做法。移除一个。参考第二个文件中的声明,如:DessertsMenu.file\u菜单
  • 与LOFO.java中共享首选项的三个声明相同
我还想猜测,您所说的“SourceNotFound”错误是一个例外,因为您试图在click listener中引用您的复选框对象。这不能这样做

相反,请尝试以下方法:

button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub SharedPreferences settings1 = getSharedPreferences(MENU_NAME, 0); switch v.getId() { case R.id.chkpie: settings1.edit().putString("pietext", "black berry pie bars").commit(); case R.id.chkpeach: //.... you see where I'm going with this right? } Intent intent = new Intent(getApplicationContext(),LOFO.class); startActivity(intent); } }); } button1.setOnClickListener(新的OnClickListener(){ @凌驾 公共void onClick(视图v){ //TODO自动生成的方法存根 SharedReferences设置1=GetSharedReferences(菜单名称,0); 开关v.getId() { 案例R.id.chkpie: setings1.edit().putString(“pietext”、“黑莓派条”).commit(); 案例R.id.chkpeach: //……你知道我要做什么了吧? } Intent Intent=新的Intent(getApplicationContext(),LOFO.class); 星触觉(意向); } }); }
请张贴stacktrace/logcat输出。和行号(如果相关)。
package net.xadtv.yoursmenu;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;



public class LOFO extends Activity{
public static final String MENU_NAME = "MyMenuFile";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.lofo);

TextView tvpeach = (TextView)findViewById(R.id.textView1);
TextView tvsquares = (TextView)findViewById(R.id.textView2);
TextView tvpie = (TextView)findViewById(R.id.textView3);

SharedPreferences settings1 = getSharedPreferences(MENU_NAME, 0);
SharedPreferences settings2 = getSharedPreferences(MENU_NAME, 0);
SharedPreferences settings3 = getSharedPreferences(MENU_NAME, 0);

tvpeach.setText(settings1.getString("peach", ""));
tvsquares.setText(settings2.getString("squares", ""));
tvpie.setText(settings3.getString("pie", ""));

}
}
button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub SharedPreferences settings1 = getSharedPreferences(MENU_NAME, 0); switch v.getId() { case R.id.chkpie: settings1.edit().putString("pietext", "black berry pie bars").commit(); case R.id.chkpeach: //.... you see where I'm going with this right? } Intent intent = new Intent(getApplicationContext(),LOFO.class); startActivity(intent); } }); }