2016-09-17 125 views
-4

所以我有2個活動 1 Android應用 - 主要 2 - 第二複選框,如果條件?

主要活動有一個RelativeLayout的,我想知道如何使用複選框使條件1個單選框其中,如果該複選框被選中我想在第二個活動中創建ImageView = VISIBLE和TextView = GONE,反之亦然,如果未選中的話。

我試圖使用共享首選項,也意圖,但我一直搞亂的地方,複選框將無關緊要或屏幕只是最終顯示空白。另一件事是,在主要活動中,我有一個onClick按鈕,已經有一個意圖。我只想知道如何使用第二個活動的複選框創建if條件。不幸的是,我會發布我的整個代碼,但我已經把它搞糟了,試圖弄清楚它到處都是這樣,而且沒有意義了。

ok我嘗試了一些東西,嘗試了一個公佈的答案,但至少我能夠清理足夠的代碼來分享現在,但它真的很糟糕,並關閉應用程序,抱歉任何奇怪的格式,新的這個

activity_main.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:id="@+id/rlay" 
    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="sam.plsnyc.com.plsign.MainActivity"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Company" 
     android:id="@+id/textView" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignBottom="@+id/editText" 
     android:textAlignment="center" 
     android:textIsSelectable="false" /> 
    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/textView" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Signage" 
     android:id="@+id/textView2" 
     android:layout_below="@+id/textView" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_alignBottom="@+id/editText2" 
     android:textAlignment="center" 
     android:textIsSelectable="false" /> 
    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText2" 
     android:layout_below="@+id/editText" 
     android:layout_alignLeft="@+id/editText" 
     android:layout_alignStart="@+id/editText" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Make Sign" 
     android:id="@+id/button" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@+id/editText2" /> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imgView" 
     android:focusable="true" 
     android:src="@drawable/logo" 
     android:scaleType="fitXY" 
     android:visibility="visible" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 
    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Use Logo" 
     android:id="@+id/logobox" 
     android:layout_below="@+id/textView2" 
     android:layout_alignParentStart="true" /> 
</RelativeLayout> 

activity_display_message.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="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_margin="0dp" 
    android:singleLine="false" 
    android:id="@+id/activity_display_message" 
    tools:context=".MainActivity" 
    android:focusableInTouchMode="false" 
    android:focusable="true" 
    android:nestedScrollingEnabled="false" 
    android:orientation="vertical" 
    android:weightSum="1"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:scaleType="fitXY" 
     android:src="@drawable/logo" 
     android:visibility="gone" 
     android:id="@+id/iV2" /> 

    <me.grantland.widget.AutofitLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:maxLines="1" 
      android:textStyle="bold" 
      android:textSize="275sp" 
      android:layout_weight=".5" 
      android:textAlignment="center" 
      android:text="LOGO" 
      android:visibility="invisible" 
      android:id="@+id/restxttop" 
      android:elegantTextHeight="false" /> 
    </me.grantland.widget.AutofitLayout> 

    <me.grantland.widget.AutofitLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight=".5" 
     android:textAlignment="center" 
     android:maxLines="1" 
     android:textSize="275sp" 
     android:text="SIGNAGE" 
     android:id="@+id/restxtbot" 
     /> 
    </me.grantland.widget.AutofitLayout> 
</LinearLayout> 

MainActivity.java

public class MainActivity extends Activity { 
    public final static String KEY_CHECKED = "chkd"; 
    CheckBox lbox; 
    Button button; 
    EditText etxt; 
    EditText etxt2; 
    String toptxt; 
    String bottxt; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main);{ 

      button = (Button) findViewById(R.id.button); 
      button.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        etxt = (EditText) findViewById(R.id.editText); 
        toptxt = etxt.getText().toString(); 
        etxt2 = (EditText) findViewById(R.id.editText2); 
        bottxt = etxt2.getText().toString(); 

        Intent i = new Intent(v.getContext(), DisplayMessageActivity.class); 
        i.putExtra("text1", toptxt); 
        i.putExtra("text2", bottxt); 
        startActivity(i); 

        lbox = (CheckBox) findViewById(R.id.logobox); 
        Intent i2 = new Intent (MainActivity.this, DisplayMessageActivity.class); 
        i2.putExtra(KEY_CHECKED, lbox.isChecked()); 
        startActivity(i2); 
       } 
      }); 
     } 
    } 
} 

DisplayMessageActivity.java

public class DisplayMessageActivity extends Activity { 
    TextView ttxt; 
    TextView btxt; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     this.getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
     this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_display_message); 

     btxt = (TextView) findViewById(R.id.restxtbot); 
     btxt.setText(getIntent().getStringExtra("text2")); 
     AutofitHelper.create(btxt); 

     ImageView image = (ImageView) findViewById(R.id.iV2); 

     Boolean chk = getIntent().getBooleanExtra(MainActivity.KEY_CHECKED, false); 
     if (chk) 
     { 
      ttxt.setVisibility(View.GONE); 
      image.setVisibility(View.VISIBLE); 
     }else{ 
      ttxt.setVisibility(View.VISIBLE); 
      ttxt = (TextView)findViewById(R.id.restxttop); 
      ttxt.setText(getIntent().getStringExtra("text1")); 
      AutofitHelper.create(ttxt); 
      ttxt.setPaintFlags(ttxt.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG); 
     } 
    } 
} 
+0

爲您提供的XML這兩個活動 –

+0

我們不能幫助你,除非你發佈你已經嘗試過的東西。這只是stackoverflow的工作方式 –

回答

0

如果從主打開第二個活動,你可以把價值器isChecked在別人的意圖在這個簡單的情況下,使靜態值