Android 从另一个函数更改动态添加的按钮背景
我会像这样在我的桌子上加一些按钮Android 从另一个函数更改动态添加的按钮背景,android,Android,我会像这样在我的桌子上加一些按钮 TableLayout table = (TableLayout) findViewById( R.id.tableLayout1 ); Bitmap bmp = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+ myPrefLocalMedia + mat_foto); final ImageButton b = new ImageButton(this);
TableLayout table = (TableLayout) findViewById( R.id.tableLayout1 );
Bitmap bmp = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+ myPrefLocalMedia + mat_foto);
final ImageButton b = new ImageButton(this);
b.setScaleType(ImageView.ScaleType.FIT_CENTER);
b.setAdjustViewBounds(true);
b.setImageBitmap(bmp);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ProgressDialog progressDialog = new ProgressDialog(SuperlineaActivity.this);
progressDialog.setMessage("Realizando el pedido...");
progressDialog.setCancelable(false);
b.setPressed(true);
if ( (miPedido == null) || ( miPedido.isEnproceso()==false) ) {
Log.e("log_","Hago post! Creo pedido!!");
Hacerpost eginpost = new Hacerpost(SuperlineaActivity.this, progressDialog);
eginpost.execute(myPrefApiPeticionMaterial,mat_id, PUESTOID, LINEAID, USUARIOID, PRODUCTOID);
} else {
Hacerput puttask = new Hacerput(SuperlineaActivity.this, progressDialog);
puttask.execute(myPrefApiPeticionMaterial,miPedido.getId());
}
}
});
row.addView( b, 300,300 );
当按下它时,它执行一些异步任务(HttpPost),并返回一个颜色代码。我需要用那个颜色代码更改按下的背景色,但我怎么知道我点击了哪个按钮
这是我接收HttpPost数据的函数:
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void erantzuna(JSONObject myJson) {
if ( miPedido == null ) {
AlertDialog alerta = new AlertDialog.Builder(SuperlineaActivity.this).create();
alerta.setTitle("PEDIDO EN PROCESO");
alerta.setMessage("El pedido se a procesado. Espera.");
alerta.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Registrado", Toast.LENGTH_SHORT).show();
}
});
alerta.show();
String kolorea = null;
String pedidoid = null;
try {
kolorea = myJson.getString("kolorea");
pedidoid = myJson.getString("pedidoid");
} catch (JSONException e) {
e.printStackTrace();
}
Log.e("log_", "Bueltan da: => " + kolorea);
Log.e("log_", "Bueltan da: => " + pedidoid);
miPedido = new Pedido(pedidoid, kolorea);
try {
if (miPedido.getKolorea().equals("kuadro-gorria")) {
//
// Here I need to change the pressed ImageButton color to RED
//
} elseif ( miPedido.getKolorea().equals("kuadro-urdina")) {
//
// Here I need to change the pressed ImageButton color to BLUE
//
} else {
//
// Here I need to change the pressed ImageButton color to transparent
//
}
} catch (Exception e) {
Log.e("log_","Boton Color => " + e.toString());
}
} else {
miPedido = null;
}
}
有什么帮助或线索吗?
提前感谢也许您应该管理动态添加的按钮,比如将它们放入列表中,或者,如果您以后不需要它们的引用,只需将该按钮交给asyncTask(比如在构造函数中)并在asyncTask的onPostExecute方法中更改其颜色即可。但是,不要忘记为任务等待响应时活动被破坏等情况做好准备,因为在这种情况下,按钮将不再存在,这也可能导致内存泄漏。Hi!谢谢你的回答,但我不能发送按钮到异步任务,因为我有太多的逻辑。我正在研究列表解决方案,但我不知道如何单击按钮来更改背景颜色好的,我知道了!谢谢你的提示!列表方法有效;)