Android:如果单击某个活动上的复选框,则可以在另一个活动上显示编辑文本

时间:2016-03-21 03:56:56

标签: android

我正在做一个客人检查应用程序。我希望有一个字段页面,其中包含用户可以选择的复选框,其中包含客人在办理登机手续时必须输入的所需信息,例如:访客姓名,访客电话号码,签名等。如果选中一个复选框活动,我希望能够在新活动上输入访客信息,该活动具有在选中每个复选框时可见的编辑文本。我已经尝试使用setOnCheckedChangeListener,但我是Android应用程序开发的新手,似乎无法让它工作。我甚至不确定我是否将代码放在正确的活动xml文件中。你能提供的任何帮助都会很棒!

这是我的guest.java代码(不是我的主要活动)

package com.example.gena.ng;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

public class Guest extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

CheckBox checkBox;
EditText editText1 = (EditText) findViewById(R.id.editText1);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_guest);

    checkBox = (CheckBox) findViewById(R.id.checkBox);
    checkBox.setOnCheckedChangeListener(this);

}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    switch (buttonView.getId()) {
        case R.id.checkBox:

            if (isChecked == true) {
                editText1.setVisibility(View.VISIBLE);
            } else {
                editText1.setVisibility(View.GONE);
            }

            break;
    }
}
}

这是我的activity_guest.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.genagizzi.ng.Guest">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:visibility="gone"
    android:text="Guest Name"
    android:ems="10"
    android:id="@+id/editText1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"/>


</RelativeLayout>

这是我的activity_fields.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
tools:context="com.example.genagizzi.ng.fields"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:orientation="horizontal">
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Please select the fields you would like to be displayed and press done."
    android:id="@+id/textView"/>

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:onClick="listOptionsCheck"
    android:text="Done"/>

</LinearLayout>

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <!-- Here you put the rest of your current view-->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Guest Name"
    android:id="@+id/checkBox"
    android:checked="false"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Drop Off Contact Name"
    android:id="@+id/checkBox2"
    android:checked="false"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Drop Off Contact Phone Number"
    android:id="@+id/checkBox3"
    android:checked="false"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Time Checked In"
    android:id="@+id/checkBox4"
    android:checked="false"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Signature Box"
    android:id="@+id/checkBox5"
    android:checked="false"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Check In/Out Button"
    android:id="@+id/checkBox6"
    android:checked="false"/>



<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Pick Up Contact Name"
    android:id="@+id/checkBox7"
    android:checked="false"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Pick Up Contact Phone Number"
    android:id="@+id/checkBox8"
    android:checked="false"/>
    </LinearLayout>
</ScrollView>
</LinearLayout>

4 个答案:

答案 0 :(得分:1)

您可以使用sharedpreference存储Checkbox的值,无论其是否被选中。

SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences("prefrence", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();

editor.putString("checkbox_value", "selected");
editor.commit();  // commit is important here.

在下一个活动中从sharedpreference检索值并在那里设置你的逻辑。

SharedPreferences shared = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
String value = (shared.getString("checkbox_value", ""));
if(value!=null && value.equals("selected")){
    // Checkbox was selected
}
else{
    // Checkbox was not selected
}

答案 1 :(得分:0)

选中此复选框后,获取变量中的值。执行intent时发送此变量。在第二页接收变量并根据选择释放编辑文本。

答案 2 :(得分:0)

当您转到下一个活动然后获取复选框的当前状态并使用putExtra()以意图传递并设置复选框的当前状态(已选中或未选中)并进入第二个活动并执行基于的操作这个值。

答案 3 :(得分:0)

您可以在第二个Activity中处理Edittext的可见性。

您需要从访客活动中删除以下代码

if (isChecked == true) {
                editText1.setVisibility(View.VISIBLE);
            } else {
                editText1.setVisibility(View.GONE);
            }

步骤:1

像这样创建一个静态类

 class CheckBoxState {

    private static Boolean isVisible = false

    public static void setVisibility(Boolean value){
    isVisible = value;
    }

    public static Boolean getVisibility(){
    return isVisible;
    }

}

第2步:

保存您的状态来自访客活动

 if (isChecked ) {
               CheckBoxState .setVisibility(true)
            } else {
               CheckBoxState .setVisibility(false)               
            }

第3步:

在您的第二个活动oncreate中执行以下操作

 if (CheckBoxState .getVisibility()) {
                editText1.setVisibility(View.VISIBLE);
            } else {
                editText1.setVisibility(View.GONE);
            }