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



我正在制作一个程序,在这个程序中,我使用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中添加一个onClickListener。现在,如果调用onClick方法,则检索复选框的状态并确定项目的价格。

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

试试这个代码

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代码设置为布局。

最新更新