0

好的,所以我有以下情况:我动态创建了一些 Editexts,当单击最后一行中的一个 EditTexts 时,我想添加另一行 Editexts。我尝试按照以下方式进行操作:

创建最后一行 EditTexts 时,我为每个人分配一个 id et.setId(997); et.setId(998); et.setId(999);

我宣布了他们中的每一个; public EditText camp1; public EditText camp2; public EditText camp3;

camp1 = (EditText) findViewById(997);
camp2 = (EditText) findViewById(998);
camp3 = (EditText) findViewById(999);

camp1.setOnClickListener(this);
camp2.setOnClickListener(this);
camp3.setOnClickListener(this);

当我尝试 case R.id.camp1: 在开关内执行此操作时,我得到"camp1 cannot be resolved or is not a field" 我做错了什么?有没有更好的方法来检测最后一个 Edittext 何时被单击并创建一个新的?

编辑:

公共类 MainActivity 扩展 Activity 实现 OnClickListener、TextWatcher {

public Button paginanoua;
// public Button calculeaza;
public Button produsnou;
public EditText camp1;
public EditText camp2;
public EditText camp3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    produsnou();
    paginanoua = (Button) findViewById(R.id.paginanoua);
    // calculeaza = (Button) findViewById(R.id.calculeaza);
    produsnou = (Button) findViewById(R.id.produsnou);
    camp1 = (EditText) findViewById(997);
    camp2 = (EditText) findViewById(998);
    camp3 = (EditText) findViewById(999);
    paginanoua.setOnClickListener(this);
    // calculeaza.setOnClickListener(this);
    produsnou.setOnClickListener(this);
    camp1.setOnClickListener(this);
    camp2.setOnClickListener(this);
    camp3.setOnClickListener(this);

}

public void onClick(View view) {
    switch(view.getId())
    {
    case R.id.paginanoua:
    ShowDialog();


    case R.id.produsnou:
        produsnou();

case R.id.997:///error
produsnou();

}
}

private void ShowDialog() {

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    dialogBuilder.setTitle("Pagina noua..");
    dialogBuilder.setMessage("Sigur doriti o pagina noua?");
    dialogBuilder.setPositiveButton("Da",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {

                    Toast.makeText(getApplicationContext(),
                            "Am inceput o lista noua", Toast.LENGTH_SHORT)
                            .show();
                    Intent intent = getIntent();
                    finish();
                    startActivity(intent);
                }

            });
    dialogBuilder.setNegativeButton("Nu",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    Toast.makeText(getApplicationContext(),
                            "Ramanem la lista curenta", Toast.LENGTH_SHORT)
                            .show();
                }
            });
    AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();
}

List<EditText> allpret = new ArrayList<EditText>();
List<EditText> allcant = new ArrayList<EditText>();
List<Float> alltotal = new ArrayList<Float>();
float totaltest = 0;

public void produsnou() {
    LinearLayout l1 = (LinearLayout) findViewById(R.id.layout1);
    EditText et = new EditText(this);
    et.setHint("Produs");
    l1.addView(et);
    et.addTextChangedListener(this);
    et.setId(997);

    LinearLayout l2 = (LinearLayout) findViewById(R.id.layout2);
    EditText et2 = new EditText(this);
    et2.setHint("Cantitate");
    et2.setInputType(InputType.TYPE_CLASS_NUMBER
            | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    et2.setId(998);

    allcant.add(et2);
    l2.addView(et2);
    et2.addTextChangedListener(this);

    LinearLayout l3 = (LinearLayout) findViewById(R.id.layout3);
    EditText et3 = new EditText(this);
    et3.setHint("Pret");
    et3.setInputType(InputType.TYPE_CLASS_NUMBER
            | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    l3.addView(et3);
    et3.setId(999);
    allpret.add(et3);
    et3.addTextChangedListener(this);
}

float temp = 0;

public void calculeaza() {
    totaltest = 0;
    String[] cant = new String[allcant.size()];

    for (int j = 0; j < allcant.size(); j++) {

        cant[j] = allcant.get(j).getText().toString();
        if (cant[j].matches("")) {
            Toast.makeText(this,
                    "Ati omis cantitatea de pe pozitia " + (j + 1),
                    Toast.LENGTH_SHORT).show();
            cant[j] = Float.toString(0);

        }
    }

    String[] pret = new String[allcant.size()];
    for (int k = 0; k < allpret.size(); k++) {
        pret[k] = allpret.get(k).getText().toString();
        if (pret[k].matches("")) {
            Toast.makeText(this,
                    "Ati omis pretul de pe pozitia " + (k + 1),
                    Toast.LENGTH_SHORT).show();
            pret[k] = Float.toString(0);

        }
    }

    for (int l = 0; l < allpret.size(); l++) {

        Float temp = Float.parseFloat(cant[l]) * Float.parseFloat(pret[l]);
        alltotal.add(temp);
        totaltest = totaltest + temp;
        // totaluri[l] = temp ; }

        TextView totalf = (TextView) findViewById(R.id.total);
        totalf.setText(String.format("Total: %.2f", totaltest));

    }
}

// Float[] totaluri = new Float[allcant.size()];

public void reload(View v) {

    Intent intent = getIntent();
    finish();
    startActivity(intent);
    calculeaza();
}

@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    // TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub
    calculeaza();

}

}

4

2 回答 2

0

你只关心最后一个editText,对吧?只需给最后一个 editText 一个 onClickListener 来创建另一个 editText。然后给新的editText onClickListener 并将其从之前的“最后一个”中删除。

像这样:

camp1 = (EditText) findViewById(997); camp2 = (EditText) findViewById(998); camp3 = (EditText) findViewById(999);

camp1.setOnClickListener(this); camp2.setOnClickListener(this); camp3.setOnClickListener(new myListener());

...

//把这个私有类和上面的东西放在同一个活动中 私有类 myListener 实现 View.OnClickListener {

    @Override
    public void onClick(View view) {
        EditText editText = new EditText(YourActivityName.this);
        editText.setOnClickListener(new myListener());
        //TODO put it in your viewGroup

        //Give the old EditText your standard onClickListener
        view.setOnClickListener(YourActivityName.this);
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
于 2013-06-11T00:13:45.763 回答
0

setId()不会向类添加任何变量,因为R.idsetId()运行时执行,但R在编译时生成。由于您正在创建动态视图,因此您需要重新考虑您的onClick()方法。您可能需要考虑使用 aListView来帮助您。您还可以使用单独的 XML 文件来设置这三个TextViews,例如row.xml.

于 2013-06-11T00:48:46.763 回答