OnClickListener无法运行android

时间:2015-10-22 09:33:27

标签: android onclicklistener

我已经在android中以编程方式创建表。 在该表中,每行有2个文本视图,1个编辑文本和1个按钮。 我必须将OnClickListner设置为按钮以删除该特定行。 但OnClickListner无法正常工作。我已经实现了这个过程而没有编辑文本。

它给出了以下警告信息。怎么解决这个?

W / InputMethodManagerService:窗口已经聚焦,忽略了焦点增益:com.android.internal.view.IInputMethodClient$Stub$Proxy@133707c3 attribute=null, token = android.os.BinderProxy@30f60fac*****

public void addRows(List MenuItem rowdata, boolean toggleColor) {

    Log.i("addrows", "CP1" + rowdata);
    tr = new TableRow(this);
    TableLayout.LayoutParams lp =
            new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                    TableLayout.LayoutParams.WRAP_CONTENT);    
    tr.setLayoutParams(lp);      

    TextView Name = new TextView(this);
    Name.setText(rowdata.getDishname());
    Name.setTextSize(18);
    Name.setTextColor(Color.parseColor("#222C31"));
    Name.setTypeface(custom);
    Name.setPadding(0,10,0,10);
    if(toggleColor) {
        Name.setBackground(getDrawable(R.drawable.border2));
    }else {
        Name.setBackground(getDrawable(R.drawable.border));
    }
    TableRow.LayoutParams nameparams =
            new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 3.2f);

    Name.setLayoutParams(dishnameparams);
    Name.setGravity(Gravity.CENTER);
    tr.addView(Name); // Adding textView to tablerow.

    TextView JobInfo = new TextView(this);
    JobInfo.setText(rowdata.getDishname());
    JobInfo.setTextSize(18);
    JobInfo.setTextColor(Color.parseColor("#222C31"));
    JobInfo.setTypeface(custom);
    JobInfo.setPadding(0,10,0,10);
    if(toggleColor) {
        JobInfo.setBackground(getDrawable(R.drawable.border2));
    }else {
        JobInfo.setBackground(getDrawable(R.drawable.border));
    }
    TableRow.LayoutParams nameparams =
            new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT, 3.2f);

    JobInfo.setLayoutParams(dishnameparams);
    JobInfo.setGravity(Gravity.CENTER);
    tr.addView(JobInfo); // Adding textView to tablerow.




    EditText SplComment = new EditText(this);
    SplComment.setText(rowdata.getSplComment());
    SplComment.setTextSize(18);
    SplComment.setTextColor(Color.parseColor("#222C31"));
    SplComment.setTypeface(custom);
    SplComment.setPadding(0, 10, 0, 10);
    if(toggleColor) {
        SplComment.setBackground(getDrawable(R.drawable.border2));
    }else {
        SplComment.setBackground(getDrawable(R.drawable.border));
    }
    TableRow.LayoutParams SplCommentparams =
            new TableRow.LayoutParams(3, TableRow.LayoutParams.MATCH_PARENT, 2.65f);
   // SplCommentparams.setMargins(0, 0, 10, 0);
    SplComment.setLayoutParams(SplCommentparams);
    SplComment.setGravity(Gravity.CENTER);
    tr.addView(SplComment); // Adding Edit text to tablerow.


    LinearLayout LL3Setup = new LinearLayout(this);
    LL3Setup.setOrientation(LinearLayout.VERTICAL);
    LL3Setup.setLayoutParams(new TableRow.LayoutParams(5, TableRow.LayoutParams.MATCH_PARENT, 0.63f));
    //LL1Setup.setPadding(20, 20, 20, 20);
    LL3Setup.setGravity(Gravity.CENTER);
    if(toggleColor) {
        LL3Setup.setBackground(getDrawable(R.drawable.border2));
    }else {
        LL3Setup.setBackground(getDrawable(R.drawable.border));
    }

    Button DeleteBtn = new Button(this);
    DeleteBtn.setText("D");
    DeleteBtn.setTextColor(Color.WHITE);
    DeleteBtn.setLayoutParams(new TableRow.LayoutParams(50, 40));
    DeleteBtn.setTypeface(custom);
    DeleteBtn.setAllCaps(false);
    DeleteBtn.setTextSize(12);
    DeleteBtn.setBackground(getDrawable(R.drawable.btn_vmd));
    DeleteBtn.setOnClickListener(DeleteListener);
    DeleteBtn.setClickable(true);
    DeleteBtn.setGravity(Gravity.CENTER);
    LL3Setup.addView(DeleteBtn);
    tr.addView(LL3Setup); // Adding button to tablerow.




    // Add the TableRow to the TableLayout
    MainTableFinish.addView(tr, new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
            TableRow.LayoutParams.MATCH_PARENT));

}


按钮列表代码:

 DeleteListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("Delete Row", "Yes");              

            UpdateTable();
        }
    };


声明:

View.OnClickListener DeleteListener;

1 个答案:

答案 0 :(得分:0)

我刚从旧的stackoverflow线程中找到了可能的解决方案。 请尝试

Delete row dynamically from table layout in android