Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用复选框可使用图像按钮onclick将textview值发送到另一个活动_Android_Android Layout_Android Intent_Android Emulator_Android Widget - Fatal编程技术网

Android 使用复选框可使用图像按钮onclick将textview值发送到另一个活动

Android 使用复选框可使用图像按钮onclick将textview值发送到另一个活动,android,android-layout,android-intent,android-emulator,android-widget,Android,Android Layout,Android Intent,Android Emulator,Android Widget,我正在制作一个程序,在这个程序中,我使用3个复选框来显示商品的变化以及3个文本视图来显示它们的价格,现在我想知道,每当用户点击复选框1,价格就会出现在文本视图1发送到下一个活动中,就像您选择的第二个活动:复选框1$2.00,我想这样做,使用图像按钮添加到订单,请写一些简短的代码,这对我来说是可能的。这里我放置main.xml代码:- <TextView android:id="@+id/regular" android:layout_width="

我正在制作一个程序,在这个程序中,我使用3个复选框来显示商品的变化以及3个文本视图来显示它们的价格,现在我想知道,每当用户点击复选框1,价格就会出现在文本视图1发送到下一个活动中,就像您选择的第二个活动:复选框1$2.00,我想这样做,使用图像按钮添加到订单,请写一些简短的代码,这对我来说是可能的。这里我放置main.xml代码:-

    <TextView
        android:id="@+id/regular"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/var1"
        android:layout_marginLeft="40dp"
        android:text="$2.00"
        android:textColor="#343434"
        android:textSize="15dip" />

    <CheckBox
        android:id="@+id/var2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cost"
        android:layout_toRightOf="@+id/var1"
        android:text="Small" />
           <TextView
        android:id="@+id/small"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/var2"
        android:layout_toRightOf="@+id/var1"
        android:layout_marginLeft="40dp"
        android:text="$1.00"
        android:textColor="#343434"
        android:textSize="15dip" />

    <CheckBox
        android:id="@+id/var3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cost"
        android:layout_toRightOf="@+id/var2"
        android:text="Large" />
           <TextView
        android:id="@+id/large"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/var3"
        android:layout_toRightOf="@+id/var2"
        android:layout_marginLeft="40dp"
        android:text="$3.00"
        android:textColor="#343434"
        android:textSize="15dip" />


    <ImageButton
        android:id="@+id/add_order"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/add_order" />

您需要在ImageButton中添加一个。现在,如果调用onClick方法,则检索复选框的状态并确定项目的价格

    <TextView
        android:id="@+id/regular"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/var1"
        android:layout_marginLeft="40dp"
        android:text="$2.00"
        android:textColor="#343434"
        android:textSize="15dip" />

    <CheckBox
        android:id="@+id/var2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cost"
        android:layout_toRightOf="@+id/var1"
        android:text="Small" />
           <TextView
        android:id="@+id/small"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/var2"
        android:layout_toRightOf="@+id/var1"
        android:layout_marginLeft="40dp"
        android:text="$1.00"
        android:textColor="#343434"
        android:textSize="15dip" />

    <CheckBox
        android:id="@+id/var3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cost"
        android:layout_toRightOf="@+id/var2"
        android:text="Large" />
           <TextView
        android:id="@+id/large"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/var3"
        android:layout_toRightOf="@+id/var2"
        android:layout_marginLeft="40dp"
        android:text="$3.00"
        android:textColor="#343434"
        android:textSize="15dip" />


    <ImageButton
        android:id="@+id/add_order"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/add_order" />
现在可以将此价格添加到用于开始下一个活动的意图中。 使用Intent类的setExtra和getExtra方法,如本文所述,尝试以下代码

    <TextView
        android:id="@+id/regular"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/var1"
        android:layout_marginLeft="40dp"
        android:text="$2.00"
        android:textColor="#343434"
        android:textSize="15dip" />

    <CheckBox
        android:id="@+id/var2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cost"
        android:layout_toRightOf="@+id/var1"
        android:text="Small" />
           <TextView
        android:id="@+id/small"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/var2"
        android:layout_toRightOf="@+id/var1"
        android:layout_marginLeft="40dp"
        android:text="$1.00"
        android:textColor="#343434"
        android:textSize="15dip" />

    <CheckBox
        android:id="@+id/var3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cost"
        android:layout_toRightOf="@+id/var2"
        android:text="Large" />
           <TextView
        android:id="@+id/large"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/var3"
        android:layout_toRightOf="@+id/var2"
        android:layout_marginLeft="40dp"
        android:text="$3.00"
        android:textColor="#343434"
        android:textSize="15dip" />


    <ImageButton
        android:id="@+id/add_order"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/add_order" />
public class MainActivity extends Activity {

private ImageButton btn;
private TextView txtSmall, txtMed,txtLarge;
private CheckBox chkSmall, chkLarge;
private String strSmall, strLarge;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Checkbox Declaration
    chkSmall = (CheckBox)findViewById(R.id.var2);
    chkLarge = (CheckBox)findViewById(R.id.var3);

    //TextView Declaration
    txtSmall = (TextView)findViewById(R.id.small);
    txtLarge = (TextView)findViewById(R.id.large);

    //ImageButton
    btn = (ImageButton)findViewById(R.id.add_order);

    //RESET VALUES
    strSmall = txtSmall.getText().toString();
    strLarge = txtLarge.getText().toString();

    btn.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

            if(chkSmall.isChecked()){
                Toast.makeText(MainActivity.this, "SMALL CHECKBOX SELECTED", Toast.LENGTH_SHORT).show();
                txtLarge.setText(txtSmall.getText().toString());
            }
            else if(chkLarge.isChecked()){
                Toast.makeText(MainActivity.this, "LARGE CHECKBOX SELECTED", Toast.LENGTH_SHORT).show();
                txtSmall.setText(txtLarge.getText().toString());
            }
           else{
                Toast.makeText(MainActivity.this, "RESET Called", Toast.LENGTH_SHORT).show();
                txtSmall.setText(strSmall);
                txtLarge.setText(strLarge);
            }

        }

    });


}

这里,您的XML代码设置为布局

Janusz我很感激,如果选中复选框1,请你写一些代码,然后文本视图1的值发送到文本视图2就是这样,因为我已经阅读了链接,现在我可以将值发送到另一个活动…..谢谢你想将值发送到另一个活动吗?兄弟,在阅读了上面的链接后,现在我可以将值发送到另一个活动,但我想每当用户将复选框,然后一个文本视图的值发送到另一个文本视图在同一活动中使用图像按钮…请写一些代码
    <TextView
        android:id="@+id/regular"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/var1"
        android:layout_marginLeft="40dp"
        android:text="$2.00"
        android:textColor="#343434"
        android:textSize="15dip" />

    <CheckBox
        android:id="@+id/var2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cost"
        android:layout_toRightOf="@+id/var1"
        android:text="Small" />
           <TextView
        android:id="@+id/small"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/var2"
        android:layout_toRightOf="@+id/var1"
        android:layout_marginLeft="40dp"
        android:text="$1.00"
        android:textColor="#343434"
        android:textSize="15dip" />

    <CheckBox
        android:id="@+id/var3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cost"
        android:layout_toRightOf="@+id/var2"
        android:text="Large" />
           <TextView
        android:id="@+id/large"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/var3"
        android:layout_toRightOf="@+id/var2"
        android:layout_marginLeft="40dp"
        android:text="$3.00"
        android:textColor="#343434"
        android:textSize="15dip" />


    <ImageButton
        android:id="@+id/add_order"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/add_order" />