我想要一个带有类的应用程序,用for循环动态创建按钮。
我调用构造函数和单击侦听器。
除了点击监听器之外的所有代码都去了,这不会改变我的活动,我能做什么?
public BotonPersonalizado(RelativeLayout layoutactual, int id, int posicionX,
int posicionY, String texto, final Context contexto) {
Button boton = new Button(contexto);
boton.setId(id);
boton.setText(texto);
boton.setTextSize(10);
boton.setMinimumHeight(5);
boton.setHeight(5);
boton.setWidth(100);
boton.setX(posicionX);
boton.setY(posicionY);
boton.setTextColor(Color.BLACK);
layoutactual.addView(boton);
我使用上下文来更改并使用Intent.FLAG_ACTIVITY_NEW_TASK来调用新活动
boton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int pasarid = v.getId();
Intent i = new Intent(contexto,Tercero.class);
i.putExtra("id", pasarid);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
contexto.startActivity(i);
}
});
}
答案 0 :(得分:1)
尝试调用此类活动,
private OnClickListener buttonclicked1 = new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(getApplicationContext(), InformationActivity.class);
startActivity(intent);
}
};
在Manifest中,
<activity android:name=".InformationActivity" android:label="@string/app_name">
- <intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
答案 1 :(得分:0)
boton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int pasarid = v.getId(); //i get the ID for the next activity
Intent i = new Intent(context,Tercero.class);
i.putExtra("id", pasarid); //i put the id to intent
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
});
}
查看此链接: