当前位置: 代码迷 >> Android >> OnClickListener 不工作 android
  详细解决方案

OnClickListener 不工作 android

热度:86   发布时间:2023-08-04 11:00:21.0

我已经在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 删除监听器;

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

  相关解决方案