OnClickListener不适用于除多个edittexts布局中的First-One之外的其他EditTexts

时间:2017-05-19 03:29:08

标签: android

我有30个EditTexts,我需要为每个设置一个OnClickListener,我已经编写了一个clickListener和下面的代码,但OnClickListener只适用于第一个EditText而且只有一次,有人可以指出一下我的代码有问题吗? 这是我的嵌套for循环,为6 x 5数组引用的所有30个EditTexts设置OnClickListener editTexts [] []:

public void displaySchedule()
{
    if(!dbHelper.isTableEmpty("schedule"))
    {
        Schedule sch = dbHelper.getSchedule();

        for(int i = 0; i < 6; i++)
        {
            for(int j = 0; j < 5; j++)
            {
                editTexts[i][j].setText(sch.scheduleArray[i][j].substring(0, 3));
                editTexts[i][j].setInputType(InputType.TYPE_NULL);
                editTexts[i][j].setOnClickListener(clickListener);
            }
        }
    }
}

这是我的clicListener:

View.OnClickListener clickListener = new View.OnClickListener()
{
    @Override
    public void onClick(View view)
    {
        final String names[] ={"A","B","C","D"};
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(ScheduleActivity.this);
        LayoutInflater inflater = getLayoutInflater();
        View convertView = inflater.inflate(R.layout.list_dialog, null);
        alertDialog.setView(convertView);
        alertDialog.setTitle("List");

        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
            }
        });

        ListView lv = (ListView) convertView.findViewById(R.id.lv);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1,
                names);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
            {
                Toast.makeText(getApplicationContext() ,names[i], Toast.LENGTH_SHORT).show();
            }
        });
        alertDialog.show();
    }
};

这是设置editTexts [] []数组的方法:

public void prepareEditTexts()
{
    editTexts[0][0] = (EditText) findViewById(R.id.sch_et_11);
    editTexts[0][1] = (EditText) findViewById(R.id.sch_et_12);
    editTexts[0][2] = (EditText) findViewById(R.id.sch_et_13);
    editTexts[0][3] = (EditText) findViewById(R.id.sch_et_14);
    editTexts[0][4] = (EditText) findViewById(R.id.sch_et_15);

    editTexts[1][0] = (EditText) findViewById(R.id.sch_et_21);
    editTexts[1][1] = (EditText) findViewById(R.id.sch_et_22);
    editTexts[1][2] = (EditText) findViewById(R.id.sch_et_23);
    editTexts[1][3] = (EditText) findViewById(R.id.sch_et_24);
    editTexts[1][4] = (EditText) findViewById(R.id.sch_et_25);

    editTexts[2][0] = (EditText) findViewById(R.id.sch_et_31);
    editTexts[2][1] = (EditText) findViewById(R.id.sch_et_32);
    editTexts[2][2] = (EditText) findViewById(R.id.sch_et_33);
    editTexts[2][3] = (EditText) findViewById(R.id.sch_et_34);
    editTexts[2][4] = (EditText) findViewById(R.id.sch_et_35);

    editTexts[3][0] = (EditText) findViewById(R.id.sch_et_41);
    editTexts[3][1] = (EditText) findViewById(R.id.sch_et_42);
    editTexts[3][2] = (EditText) findViewById(R.id.sch_et_43);
    editTexts[3][3] = (EditText) findViewById(R.id.sch_et_44);
    editTexts[3][4] = (EditText) findViewById(R.id.sch_et_45);

    editTexts[4][0] = (EditText) findViewById(R.id.sch_et_51);
    editTexts[4][1] = (EditText) findViewById(R.id.sch_et_52);
    editTexts[4][2] = (EditText) findViewById(R.id.sch_et_53);
    editTexts[4][3] = (EditText) findViewById(R.id.sch_et_54);
    editTexts[4][4] = (EditText) findViewById(R.id.sch_et_55);

    editTexts[5][0] = (EditText) findViewById(R.id.sch_et_61);
    editTexts[5][1] = (EditText) findViewById(R.id.sch_et_62);
    editTexts[5][2] = (EditText) findViewById(R.id.sch_et_63);
    editTexts[5][3] = (EditText) findViewById(R.id.sch_et_64);
    editTexts[5][4] = (EditText) findViewById(R.id.sch_et_65);
}

1 个答案:

答案 0 :(得分:0)

您是如何检查View.OnClickListener的?

您应该检查View.OnClickListener作为下面的代码来确认:

 @Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.editext1:
            Log.d("Called me", "onClicked " + R.id.editext1);
            break;
        case R.id.editext2:
            Log.d("Called me", "onClicked " + R.id.editext2);
            break;
        ...
        default:
            break;

    }
}