如何在单击时设置动态生成的textview背景

时间:2013-04-26 10:27:10

标签: android

如何在单击动态创建的按钮时设置背景可绘制:

enter image description here

我使用上面的代码来创建动态按钮并跟踪特定按钮的点击:

for (int i = 1; i<8; i++)
{

    if(i==7)
    {
        btn = custom.myButton(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT), null, i+30, "...");
        btn.setTag(i);
        linear_paging.addView(btn);
    }
    else
    {
        btn = custom.myButton(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT), null, i+30, ""+i);
        btn.setTag(i);
        linear_paging.addView(btn);
    }


    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Toast.makeText(Earned_New.this, v.getTag()+" clicked", Toast.LENGTH_SHORT).show();
            v.setBackgroundDrawable(getResources().getDrawable(R.drawable.black_rounded_background));
        }
    });
}

最后我得到了解决方案 。请按照以下流程完成任务,并根据您的要求进行修改,一定会对您有所帮助: 1.)在onResume()方法中执行此操作:

new AsynDriverEarned().execute();

2。)CONST.EARNED_LISTVIEW_SIZE = 10 //单页中的记录数

3。)这是内部类(AsyncTask)

class AsynDriverEarned extends AsyncTask<String, Void, String>{
        ProgressDialog dialog=null;
        JSONObject jsonObject;
        JSONObject jsonUserDetail;
        String response="";
        String result="";
        SharedPreferences settingPref;

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            dialog=new ProgressDialog(Earned_New.this);
            dialog.setMessage("Loading...");
        //  dialog.setCancelable(false);
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

            Log.i("DRIVER_ID", CONST.LOGIN_ID);
            postParameters.add(new BasicNameValuePair("DRIVER_ID", CONST.LOGIN_ID));

            try{
                response=CustomHttpClient.executeHttpPost(CUSTOM_URL.Common_Url+"mobile_driver_earned.php", postParameters);
                Log.i("response:", ""+response);
                result = "OK";
                if(response!=null){

                    try {
                        jsonObject = new JSONObject(response);
                        if(jsonObject.getBoolean("SUCCESS")){

                            pound_value_string = jsonObject.getString("TOTAL_EARNING");

                            result = "OK";
                            JSONArray jsonDriverArray = jsonObject.getJSONArray("DRIVER_ARRAY");
                            Log.i("jsonDriverArray:", ""+jsonDriverArray);

                            date_time_list = new ArrayList<String>();
                            drop_loc_list = new ArrayList();
                            passenger_names_list = new ArrayList();
                            total_earn_list = new ArrayList();

                            for (int i = 0; i < jsonDriverArray.length(); i++)
                            {
                                date_time_list.add(jsonDriverArray.getJSONObject(i).getString("BOOKING_DATE"));
                                drop_loc_list.add(jsonDriverArray.getJSONObject(i).getString("PASSENGER_DROP_LOCATION"));
                                passenger_names_list.add(jsonDriverArray.getJSONObject(i).getString("PASSENGER_NAME"));
                                total_earn_list.add(jsonDriverArray.getJSONObject(i).getString("EARNING"));
                            }

                        }
                        else{
                            result="FAILURE";
                            earner_error_message = jsonObject.getString("ERROR");
                            Log.i("earner_error_message", earner_error_message);
                        }
                    }
                    catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        result = e.getMessage();
                    }
                }else{
                    result = "Timed Out!";
                }

            }catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            //  result = e.getMessage();                
            }

            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            dialog.dismiss();

            if(result.equalsIgnoreCase("OK"))
            {
                pound_value.setText(pound_value_string);

                ArrayList<String> temp_date_time_list = new ArrayList<String>();
                ArrayList<String> temp_drop_loc_list = new ArrayList();
                ArrayList<String> temp_passenger_names_list = new ArrayList();
                ArrayList<String> temp_total_earn_list = new ArrayList();

                int start_number_of_records = (CONST.EARNED_LISTVIEW_SIZE*(1-1)); // no_of_records*tag_value
                Log.i("start_number_of_records", ""+start_number_of_records);
                int end_number_of_records = (((CONST.EARNED_LISTVIEW_SIZE*(1-1))+CONST.EARNED_LISTVIEW_SIZE)-1); //(no_of_records*tag_value+no_of_records)-1
                Log.i("end_number_of_records", ""+end_number_of_records);

                if (end_number_of_records<date_time_list.size())
                {   
                    Log.i("ENTER:", "FIRST");

                    for (int j = start_number_of_records; j < end_number_of_records+1; j++)
                    {
                        temp_date_time_list.add(date_time_list.get(j));
                        temp_drop_loc_list.add(drop_loc_list.get(j));
                        temp_passenger_names_list.add(passenger_names_list.get(j));
                        temp_total_earn_list.add(total_earn_list.get(j));
                    }
                    Log.i("temp_date_time_list.size()", ""+temp_date_time_list.size());
                    setListAdapter(temp_date_time_list,temp_drop_loc_list,temp_passenger_names_list,temp_total_earn_list);
                    adapter.notifyDataSetChanged();
                 }

                else
                {
                    Log.i("ENTER:", "SECOND");
                    for (int j = start_number_of_records; j < date_time_list.size(); j++)
                    {
                        temp_date_time_list.add(date_time_list.get(j));
                        temp_drop_loc_list.add(drop_loc_list.get(j));
                        temp_passenger_names_list.add(passenger_names_list.get(j));
                        temp_total_earn_list.add(total_earn_list.get(j));
                    }
                    Log.i("temp_date_time_list.size()", ""+temp_date_time_list.size());
                    setListAdapter(temp_date_time_list,temp_drop_loc_list,temp_passenger_names_list,temp_total_earn_list);
                    adapter.notifyDataSetChanged();
                }

                int num = date_time_list.size();
                Log.i("num", ""+num);
                int counter=1;

                while(num>CONST.EARNED_LISTVIEW_SIZE)
                {
                    num = num-CONST.EARNED_LISTVIEW_SIZE; // num = 32-5==27
                    counter++;
                    Log.i("counter", ""+counter);
                    continue;
                }

            for (int i = 1; i<counter+1; i++)
            {
                    btn = custom.myButton(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT), null, i+30, ""+i);
                    btn.setTag(i);
                    linear_paging.addView(btn);

                btn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_selector));

                        ArrayList<String> temp_date_time_list = new ArrayList<String>();
                        ArrayList<String> temp_drop_loc_list = new ArrayList();
                        ArrayList<String> temp_passenger_names_list = new ArrayList();
                        ArrayList<String> temp_total_earn_list = new ArrayList();

                        int start_number_of_records = (CONST.EARNED_LISTVIEW_SIZE*((Integer) v.getTag()-1)); // no_of_records*tag_value
                    //  Log.i("start_number_of_records", ""+start_number_of_records);
                        int end_number_of_records = (((CONST.EARNED_LISTVIEW_SIZE*((Integer) v.getTag()-1))+CONST.EARNED_LISTVIEW_SIZE)-1); //(no_of_records*tag_value+no_of_records)-1
                    //  Log.i("end_number_of_records", ""+end_number_of_records);


                    if (end_number_of_records<date_time_list.size())
                    {   
                        Log.i("ENTER:", "FIRST");

                        for (int j = start_number_of_records; j < end_number_of_records+1; j++)
                        {
                            temp_date_time_list.add(date_time_list.get(j));
                            temp_drop_loc_list.add(drop_loc_list.get(j));
                            temp_passenger_names_list.add(passenger_names_list.get(j));
                            temp_total_earn_list.add(total_earn_list.get(j));
                        }
                        Log.i("temp_date_time_list.size()", ""+temp_date_time_list.size());

                        setListAdapter(temp_date_time_list,temp_drop_loc_list,temp_passenger_names_list,temp_total_earn_list);
                        adapter.notifyDataSetChanged();
                     }

                    else
                    {
                        Log.i("ENTER:", "SECOND");
                        for (int j = start_number_of_records; j < date_time_list.size(); j++)
                        {
                            temp_date_time_list.add(date_time_list.get(j));
                            temp_drop_loc_list.add(drop_loc_list.get(j));
                            temp_passenger_names_list.add(passenger_names_list.get(j));
                            temp_total_earn_list.add(total_earn_list.get(j));
                        }

                        Log.i("temp_date_time_list.size()", ""+temp_date_time_list.size());

                        setListAdapter(temp_date_time_list,temp_drop_loc_list,temp_passenger_names_list,temp_total_earn_list);
                        adapter.notifyDataSetChanged();
                    }
                    }
                });
                }
            }
            else
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(Earned_New.this);
                builder.setMessage(earner_error_message);
                builder.setCancelable(false);
                LayoutInflater inflater = getLayoutInflater();
                View vw = inflater.inflate(R.layout.custom_title, null);
                builder.setCustomTitle(vw);
                builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                        finish();
                    }
                });

                builder.show();
            }
        }   
    }

1 个答案:

答案 0 :(得分:0)

您可以将选择器用作该视图的可绘制对象。 这是样品..

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@android:color/darker_gray" android:state_pressed="true"></item>
    <item android:drawable="@android:color/darker_gray" android:state_selected="true"></item>
    <item android:drawable="@android:color/darker_gray" android:state_checked="true"></item>
    <item android:drawable="@android:color/transparent" ></item>

</selector>

您可以更改上面的颜色。 通过使用RadioButton代替Button,您可以轻松实现这一目标。如果你不能这样做,我会提供更多信息。