【问题标题】:How to show multiple checkbox information in another activity when these checkboxes are clicked?单击这些复选框时,如何在另一个活动中显示多个复选框信息?
【发布时间】:2014-02-13 15:35:54
【问题描述】:

我们想要做的是将 Desserts Menu 中选中复选框的信息显示到 List Of Final Order (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="#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"  />

这是我们的 DessertsMenu.java:

package net.xadtv.yoursmenu;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;




public class DessertsMenu extends Activity{
Button button1;
CheckBox chkpie;
CheckBox chkpeach;
CheckBox chksquares;
public static final String MENU_NAME = "MyMenuFile";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.desserts);
button1 = (Button) findViewById(R.id.button1);
chkpie = (CheckBox) findViewById(R.id.chkpie);
chkpeach = (CheckBox) findViewById(R.id.chkpeach);
chksquares = (CheckBox) findViewById(R.id.chksquares);
button1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

if(chkpie.isChecked());
SharedPreferences settings1 = getSharedPreferences(MENU_NAME, 0);
SharedPreferences.Editor pie = settings1.edit();
pie.putString("pietext", "black berry pie bars");

pie.commit();

if(chkpeach.isChecked());
SharedPreferences settings2 = getSharedPreferences(MENU_NAME, 0);
SharedPreferences.Editor peach = settings2.edit();
peach.putString("peachtext", "peach crumbbars");

peach.commit();

if(chksquares.isChecked());
SharedPreferences settings3 = getSharedPreferences(MENU_NAME, 0);
SharedPreferences.Editor squares = settings3.edit();
squares.putString("squarestext", "snickery squares");

squares.commit();

Intent intent = new Intent(getApplicationContext(),LOFO.class);
startActivity(intent);
}
});

}

}

这是我们要放置点击的复选框信息的地方,即最终订单活动列表。

这是我们的 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" />

这是我们的 LOFO.java:

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", ""));

}
}

现在的问题是,一旦我们运行它然后选中其中一个复选框,就会发生错误(未找到源)。

非常感谢任何帮助。 :)

【问题讨论】:

  • 请发布 stacktrace/logcat 输出。以及相关的行号。

标签: android checkbox sharedpreferences


【解决方案1】:

几件事:

  • 你有:if(chkpie.isChecked());看到最后那个分号了吗?这意味着 if 语句是无用的
  • 您还有两个 FILE_MENU 声明。这是草率和糟糕的做法。删除一个。像这样引用第二个文件中的声明:DessertsMenu.FILE_MENU
  • 与共享首选项的 LOFO.java 中的三个声明相同

我还将猜测您所说的“找不到源”错误是一个异常,因为您试图在单击侦听器中引用您的复选框对象。不能这样做。

试试这个:

button1.setOnClickListener(new OnClickListener() { @覆盖 公共无效 onClick(查看 v){ // TODO 自动生成的方法存根 SharedPreferences settings1 = getSharedPreferences(MENU_NAME, 0); 切换 v.getId() { 案例 R.id.chkpie: settings1.edit().putString("pietext", "黑莓派条").commit(); 案例 R.id.chkpeach: //....你明白我的意思了吗? } Intent 意图 = new Intent(getApplicationContext(),LOFO.class); 开始活动(意图); } }); }

【讨论】:

    猜你喜欢
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 2020-01-19
    • 2021-06-08
    • 1970-01-01
    相关资源
    最近更新 更多