Button.performClick()不起作用

时间:2013-06-13 15:52:44

标签: java android

我需要创建一个包含一些视图的linearLayout,当点击一个按钮时,thsi按钮被另一个活动中的动作触发,所以我使用了performClick,但它似乎不起作用;这是我的代码:

Button click = new Button(rootView.getContext());
            SharedPreferences participant;
            Editor editor;

            SharedPreferences visible;
            Editor vis;
            participant = rootView.getContext().getSharedPreferences("participant", rootView.getContext().MODE_PRIVATE);
            visible = rootView.getContext().getSharedPreferences("visible", rootView.getContext().MODE_PRIVATE);

            editor = participant.edit();
            final String name= participant.getString("key", "toto");
            final String view = participant.getString("view","non");
            if(view.equalsIgnoreCase("yes")) click.performClick();

            click.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(final View v) {


                    // Creating a new LinearLayout
                    final LinearLayout ln = new LinearLayout(v.getContext());

                    // Setting the orientation to horizontal
                    ln.setOrientation(LinearLayout.HORIZONTAL);


                    formbis.addView(ln);

                    TextView tv1 = new TextView(v.getContext());
                    tv1.setText(name);
                    tv1.setTextSize(14);
                    tv1.setTypeface(null, Typeface.BOLD);
                    tv1.setPadding(0, 15, 0, 10);
                    tv1.setLayoutParams(new LayoutParams(
                            500,
                            LayoutParams.WRAP_CONTENT));
                    ln.addView(tv1);

                    final ImageButton edit = new ImageButton(v.getContext());
                    Drawable checked = getResources().getDrawable( R.drawable.content_edit );
                    edit.setImageDrawable(checked);
                    edit.setLayoutParams(new LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT));
                    edit.setBackgroundColor(Color.WHITE);
                    edit.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {


                           i = new Intent(v.getContext(), EditParticipantActivity.class);
                            startActivity(i);

                        }

                    });
                    ln.addView(edit);

                    final ImageButton delete = new ImageButton(v.getContext());
                    Drawable deleted = getResources().getDrawable( R.drawable.content_discard );
                    delete.setImageDrawable(deleted);
                    delete.setLayoutParams(new LayoutParams(
                            LayoutParams.WRAP_CONTENT,
                            LayoutParams.WRAP_CONTENT));
                    delete.setBackgroundColor(Color.WHITE);
                    delete.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                             Toast.makeText(v.getContext(),
                                        "Deleted ", Toast.LENGTH_LONG).show();

                           formbis.removeView(ln);

                        }

                    });
                    ln.addView(delete);
                    ln.setVisibility(View.GONE);
                    if(view.equalsIgnoreCase("yes"))ln.setVisibility(View.VISIBLE);

                }
            });

修改

这里(另一个Activity)我将值设置为yes,以便将linearLayout添加到第一个活动的视图中

Button members = (Button) findViewById(R.id.submit);
        members.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                name = firstname.getText().toString();

                editor.putString("key", name);
                editor.commit();

                editor.putString("view", "yes");
                editor.commit();

                i = new Intent(v.getContext(), ManageActivity.class);
                startActivity(i);
                //finish();

            }

});

1 个答案:

答案 0 :(得分:7)

首先定义onClickListener

然后在定义if(view.equalsIgnoreCase("yes")) click.performClick();代码后调用click.onClickListner行。