【问题标题】:Checkbox if condition?复选框如果条件?
【发布时间】:2017-01-25 10:15:28
【问题描述】:

所以我有一个带有 2 个活动的 android 应用 1 - 主要 2 - 秒

主要活动在 RelativeLayout 中有 1 个单个复选框我想知道如何使用该复选框来创建一个条件,如果选中该复选框,我想在第二个活动中设置 ImageView = VISIBLE 和 TextView = GONE 并且如果未选中,反之亦然。

我曾尝试使用共享首选项和意图,但我总是在某个地方搞砸了,复选框无关紧要,或者屏幕最终显示为空白。另一件事是,在主要活动中,我有一个具有 onClick 并且已经具有意图的按钮。我只想知道如何使用第二个活动的复选框创建 if 条件。可悲的是,我会发布我的整个代码,但我把它弄得很糟糕,试图弄清楚它到处都是,而且不再有意义了。

好的,我尝试了一些事情,尝试了发布的答案,但至少我能够清理代码足以现在分享,但它非常糟糕并关闭了应用程序,抱歉任何奇怪的格式,这是新的

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);
        }
    }
}

【问题讨论】:

  • 为这两个活动提供您的 xml
  • 除非您发布您已经尝试过的内容,否则我们无法为您提供帮助。这正是 stackoverflow 的工作方式

标签: android android-activity checkbox


【解决方案1】:

如果您从主要活动中打开第二个活动,您可以将 isChecked 值放在意图中,否则在这种简单的情况下将值设为静态

【讨论】:

    猜你喜欢
    • 2015-06-18
    • 1970-01-01
    • 2020-08-09
    • 2012-12-18
    • 2016-07-10
    • 2021-10-26
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    相关资源
    最近更新 更多