所以我有一个编程按钮,按下时添加了edittext,spinner和一个删除按钮,我想要做的是当我点击删除按钮时删除edittext,微调器和按钮。我的代码不起作用,我看不到日志返回。有人可以告诉我应该在我的代码上点击On Click事件吗?
public LinearLayout createFields(final int id){
LinearLayout llLayout = new LinearLayout(Customer.this);
llLayout.setOrientation(LinearLayout.HORIZONTAL);
TableRow.LayoutParams spinParams = new TableRow.LayoutParams(0, 50, 1f);
spinParams.setMargins(0, 5, 0, 0);
TableRow.LayoutParams params = new TableRow.LayoutParams(0, 50, 0.5f);
params.setMargins(5, 5, 0, 0);
final Spinner spinner = new Spinner(Customer.this);
spinner.setLayoutParams(spinParams);
spinner.setBackgroundResource(R.drawable.roundedittext);
spinner.setGravity(Gravity.CENTER);
final EditText etQty = new EditText(Customer.this);
etQty.setLayoutParams(params);
etQty.setBackgroundResource(R.drawable.roundedittext);
etQty.setHint("Quantity");
etQty.setGravity(Gravity.CENTER);
final EditText etPrice = new EditText(Customer.this);
etPrice.setLayoutParams(params);
etPrice.setBackgroundResource(R.drawable.roundedittext);
etPrice.setHint("Price");
etPrice.setGravity(Gravity.CENTER);
final Button btRemove = new Button(Customer.this);
btRemove.setLayoutParams(params);
//btRemove.setBackgroundResource(R.drawable.roundedittext);
btRemove.setText("Remove");
btRemove.setGravity(Gravity.CENTER);
ArrayList<String>itemList = new ArrayList<String>();
Cursor cursor = db.read("SELECT DISTINCT item_name from item_tb");
itemList.add("Select Item");
while(cursor.moveToNext()){
itemList.add(cursor.getString(cursor.getColumnIndex("item_name")));
}
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(Customer.this,R.layout.spinner_layout, itemList);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setPrompt("Select Item");
spinner.setAdapter(spinnerArrayAdapter);
btRemove.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("REMOVE CLICK", "Tapped ");
}
});
etQty.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence c, int arg1, int arg2, int arg3) {
SampleObj obj = new SampleObj();
String itemz = spinner.getSelectedItem().toString();
String itemz2 = db.getValue("SELECT itemID from item_tb where item_name = '"+itemz+"'");
obj.item = itemz2;
obj.qty = c.toString();
obj.price = etPrice.getText().toString();
list.set(id, obj);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
etPrice.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence c, int arg1, int arg2, int arg3) {
SampleObj obj = new SampleObj();
String itemz = spinner.getSelectedItem().toString();
String itemz2 = db.getValue("SELECT itemID from item_tb where item_name = '"+itemz+"'");
obj.item = itemz2;
obj.qty = etQty.getText().toString();
obj.price = c.toString();
list.set(id, obj);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
}
});
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long arg3) {
SampleObj obj = new SampleObj();
String itemz = spinner.getSelectedItem().toString();
String itemz2 = db.getValue("SELECT itemID from item_tb where item_name = '"+itemz+"'");
obj.item = itemz2;
obj.qty = etQty.getText().toString();
obj.price = etQty.getText().toString();
Customer.list.set(id, obj);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
llLayout.addView(spinner);
llLayout.addView(etQty);
llLayout.addView(etPrice);
llLayout.addView(btRemove);
return llLayout;
}
答案 0 :(得分:2)
只需在代码中全局定义Edittext,微调器和按钮,然后点击按钮点击您设置的设置。
所以这样做。
在代码中全局定义。
Spinner spinner = new Spinner(Customer.this);
EditText etQty = new EditText(Customer.this);
EditText etPrice = new EditText(Customer.this);
然后在onclick of remove试试这个。
btRemove.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("Inside Remove button's click event");
spinner.setVisibility(View.gone);
etPriceset.Visibility(View.gone);
etQty.Visibility(View.gone);
}
});
答案 1 :(得分:1)
我想你错过@Override
onClick事件。所以添加这个可能会得到结果。
试试这个
btRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("REMOVE CLICK", "Tapped ");
}
});