2016-03-21 15 views
0

私はゲストのチェックをアプリで作っています。私は、ゲスト名、ゲストの電話番号、署名など、ゲストがチェックイン時に入力する必要がある情報を持つチェックボックスを持つフィールドページを持っていたいと思います。各チェックボックスが選択されたときに表示される編集テキストを持つ新しいアクティビティに関するゲスト情報を入力できるようにしたいと考えています。私はsetOnCheckedChangeListenerを使用してみましたが、私はアンドロイドアプリの開発に新しく、それを働かせることはできません。私は正しい活動のXMLファイルにコードを入れているのかどうかはわかりません。あなたが提供できるどんな助けも素晴らしいでしょう!ここでAndroid:1つのアクティビティのチェックボックスをクリックすると、別のアクティビティの編集テキストが表示されます

はここに(私のメインの活動をイマイチ)私の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> 
です

答えて

0

チェックボックスが選択されたら、変数の値を取得します。インテントを実行している間にこの変数を送信します。 2番目のページで、変数を受け取り、選択に基づいて編集テキストを解放します。

0

チェックボックスの現在の状態を取得し、putExtra()を使用してインテントとともに渡し、チェックボックスの現在の状態をチェックしたりチェックしたりして、2番目のアクティビティに入り、アクションベースこの値。

0

第2アクティビティの編集テキストの表示を処理できます。

あなたはユーザーレビュー活動

から次のコードを削除する必要が

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のような1つの静的クラスを作成します。ゲストアクティビティからのあなたの状態

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

ステップ3:あなたの第二の活動で

はあなたは、その選択したかどうかCheckboxの値を格納するのにsharedpreferenceを使用することができ、次の

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

を行うのonCreate。次の活動に

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 
} 
+0

これを実装する方法はわかりません。私はJavaプログラミングの初心者です。だから私のフィールドのクラスでは、チェックボックスの値を格納する最初のコードを含むsavedPreferencesと呼ばれるメソッドがチェックボックスに含まれます。しかし、チェックボックスの値はどのように保存されていますか?どこのチェックボックスを保存するかを知るために、どこにxmlレイアウトのidにチェックボックスを割り当てますか? – Gena

関連する問題