【问题标题】:use checkbox to send textview value to another activity using image button onclick使用复选框使用图像按钮 onclick 将 textview 值发送到另一个活动
【发布时间】:2012-10-11 06:46:46
【问题描述】:

我正在制作一个程序,其中我使用 3 个复选框来表示项目变化以及 3 个文本视图作为价格,现在我想知道,每当用户单击复选框 1 时,价格就会出现在 textview1 中发送到下一个活动,就像在您选择的第二个活动中一样:checkbox1 $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" />

【问题讨论】:

    标签: android android-layout android-intent android-emulator android-widget


    【解决方案1】:

    您需要将onClickListener 添加到您的ImageButton。现在,如果调用 onClick 方法,则检索复选框的状态并确定商品的价格。

    现在可以将此价格添加到您用于启动下一个活动的 Intent 中。 使用 Intent 类的 setExtra 和 getExtra 方法,如Tutorial中所述

    【讨论】:

    • Janusz 我很感激,如果checkbox1被选中然后textview1的值发送到textview2就是这样,你能不能写一些代码,因为我已经阅读了链接,现在我可以将值发送到另一个活动。 ....谢谢
    • 您想将值发送到另一个活动吗?
    • 兄弟,读完上面的链接后,现在我可以将值发送到另一个活动,但我希望每当用户选中复选框,然后使用图像按钮将一个文本视图的值发送到同一活动中的另一个文本视图...请写一些代码
    【解决方案2】:

    试试这个代码

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

    Here 您的 XML 代码设置为布局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 2011-11-26
      相关资源
      最近更新 更多