我需要创建一个包含一些视图的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);
}
});
编辑
在这里(另一个活动),我将值设置为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();
}
(});
最佳答案
首先定义onClickListener
。
然后在定义if(view.equalsIgnoreCase("yes")) click.performClick();
代码后调用click.onClickListner
行。