繁体   English   中英

在Android中更改字段的背景色

[英]Changing background colour of a field in android

以下是我的Java文件的代码:

package samples.employeedirectory;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class EmployeeDetails extends ListActivity {

    protected TextView employeeNameText;
    protected TextView titleText;
    protected List<EmployeeAction> actions;
    protected EmployeeActionAdapter adapter;
    protected int employeeId;
    protected int managerId;
    protected TextView availabilityText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.employee_details);

        employeeId = getIntent().getIntExtra("EMPLOYEE_ID", 0);
        SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT emp._id, emp.firstName, emp.lastName, emp.experience, emp.techskills, emp.currentpro, emp.title, emp.officePhone, emp.cellPhone, emp.email, emp.availability, emp.modelskills, emp.managerId, mgr.firstName managerFirstName, mgr.lastName managerLastName FROM employee emp LEFT OUTER JOIN employee mgr ON emp.managerId = mgr._id WHERE emp._id = ?", 
                new String[]{""+employeeId});

        if (cursor.getCount() == 1)
        {
            cursor.moveToFirst();

            employeeNameText = (TextView) findViewById(R.id.employeeName);
            employeeNameText.setText(cursor.getString(cursor.getColumnIndex("firstName")) + " " + cursor.getString(cursor.getColumnIndex("lastName")));

            titleText = (TextView) findViewById(R.id.title);
            titleText.setText(cursor.getString(cursor.getColumnIndex("title")));




            actions = new ArrayList<EmployeeAction>();



            String officePhone = cursor.getString(cursor.getColumnIndex("officePhone"));
            if (officePhone != null) {
                actions.add(new EmployeeAction("Call office", officePhone, EmployeeAction.ACTION_CALL));
            }

            String cellPhone = cursor.getString(cursor.getColumnIndex("cellPhone"));
            if (cellPhone != null) {
                actions.add(new EmployeeAction("Call mobile", cellPhone, EmployeeAction.ACTION_CALL));
                actions.add(new EmployeeAction("SMS", cellPhone, EmployeeAction.ACTION_SMS));
            }

            String email = cursor.getString(cursor.getColumnIndex("email"));
            if (email != null) {
                actions.add(new EmployeeAction("Email", email, EmployeeAction.ACTION_EMAIL));
            }

            String available = cursor.getString(cursor.getColumnIndex("availability"));
            if (available != null) {
                actions.add(new EmployeeAction("Availability", available, -1));
            }

            String experienc = cursor.getString(cursor.getColumnIndex("experience"));
            if (experienc != null) {
                actions.add(new EmployeeAction("Experience(in years)", experienc, -1));
            }

            String modskills = cursor.getString(cursor.getColumnIndex("modelskills"));
            if (modskills != null) {
                actions.add(new EmployeeAction("Modelling skills",modskills, -1));
            }

            String techskills = cursor.getString(cursor.getColumnIndex("techskills"));
            if (techskills != null) {
                actions.add(new EmployeeAction("Technical skills",techskills, -1));
            }
            String currpro = cursor.getString(cursor.getColumnIndex("currentpro"));
            if (currpro != null) {
                actions.add(new EmployeeAction("Current project",currpro, -1));
            }

            managerId = cursor.getInt(cursor.getColumnIndex("managerId"));
            if (managerId>0) {
                actions.add(new EmployeeAction("View manager", cursor.getString(cursor.getColumnIndex("managerFirstName")) + " " + cursor.getString(cursor.getColumnIndex("managerLastName")), EmployeeAction.ACTION_VIEW));
            }

            cursor = db.rawQuery("SELECT count(*) FROM employee WHERE managerId = ?", 
                    new String[]{""+employeeId});
            cursor.moveToFirst();
            int count = cursor.getInt(0);
            if (count>0) {
                actions.add(new EmployeeAction("View direct reports", "(" + count + ")", EmployeeAction.ACTION_REPORTS));
            }

            adapter = new EmployeeActionAdapter();
            setListAdapter(adapter);
        }

    }

    public void onListItemClick(ListView parent, View view, int position, long id) {

        EmployeeAction action = actions.get(position);

        Intent intent;
        switch (action.getType()) {

            case EmployeeAction.ACTION_CALL:  
                Uri callUri = Uri.parse("tel:" + action.getData());  
                intent = new Intent(Intent.ACTION_CALL, callUri); 
                startActivity(intent);
                break;

            case EmployeeAction.ACTION_EMAIL:  
                intent = new Intent(Intent.ACTION_SEND);
                intent.setType("plain/text");
                intent.putExtra(Intent.EXTRA_EMAIL, new String[]{action.getData()});
                startActivity(intent);
                break;

            case EmployeeAction.ACTION_SMS:  
                Uri smsUri = Uri.parse("sms:" + action.getData());  
                intent = new Intent(Intent.ACTION_VIEW, smsUri); 
                startActivity(intent);
                break;

            case EmployeeAction.ACTION_REPORTS:  
                intent = new Intent(this, DirectReports.class);
                intent.putExtra("EMPLOYEE_ID", employeeId);
                startActivity(intent);
                break;

            case EmployeeAction.ACTION_VIEW:  
                intent = new Intent(this, EmployeeDetails.class);
                intent.putExtra("EMPLOYEE_ID", managerId);
                startActivity(intent);
                break;
        }
    }    

    class EmployeeActionAdapter extends ArrayAdapter<EmployeeAction> {

        EmployeeActionAdapter() {
            super(EmployeeDetails.this, R.layout.action_list_item, actions);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            EmployeeAction action = actions.get(position);
            LayoutInflater inflater = getLayoutInflater();
            View view = inflater.inflate(R.layout.action_list_item, parent, false);
            TextView label = (TextView) view.findViewById(R.id.label);
            label.setText(action.getLabel());
            TextView data = (TextView) view.findViewById(R.id.data);
            data.setText(action.getData());
            return view;
        }

    }

}

以下是相应的xml文件的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#505050"
    android:padding="8px">
    <TextView
        android:id="@+id/employeeName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"
        android:textSize="50px"/>
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"/>

</LinearLayout>

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

我需要为某些字段使用不同的背景色,例如可用性或技术技能。 我需要您的帮助以进行更改吗?

放置一个全局标志(或在显示之前检查值),然后进行相应的更改-必须根据您的可见性更改来设置一个值? 因此,在为该视图设置背景之前,请检查该字段。

说场是int状态;

if(status == 1){

View.setBackgroundresource(R.drawable.image1);
}else{
View.setBackgroundresource(R.drawable.image2);
}

这是一个示例游标适配器类,该类演示记录的获取并根据显示的不同字符串的值将它们显示在列表中:

private class CurAdapter extends CursorAdapter{


        boolean imageExists;


        public CurAdapter(Context context, Cursor c, int flags) {
            super(context, c, flags);

        }


        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            TextView tv = (TextView) view.findViewById(R.id.textView1);
            TextView tv1 = (TextView) view.findViewById(R.id.textView2);
            ImageView iv = (ImageView) view.findViewById(R.id.imageView1);


            //  String isAdhoc = (cursor.getString(cursor.getColumnIndexOrThrow("isAdhoc")));
            String name = (cursor.getString(cursor.getColumnIndexOrThrow("Name")));
            String image = cursor.getString(cursor.getColumnIndexOrThrow("imageUri"));
            String manuplate = dateConvert(cursor.getString(cursor.getColumnIndexOrThrow("Date")));


            //  if(isAdhoc.equals("1")){

            tv.setText(name);


            if(manuplate.contains("0001")){
                manuplate = manuplate.replace("0001", "");
            }
            tv1.setText((manuplate));
            tv.setTypeface(tf, Typeface.BOLD);
            tv1.setTypeface(tf1); 

            //  System.out.println("Image Uri is:"+image);


            if(image.contains("jpg") || image.contains("png")){
                File f = new File(image);
                Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
                iv.setImageBitmap(bmp); 




            }else{
                Uri IMAGE_URI = null;
                IMAGE_URI = Uri.parse(image);
                IMAGE_URI= Uri.withAppendedPath(IMAGE_URI, Contacts.Photo.CONTENT_DIRECTORY);
                try{
                    iv.setImageURI(IMAGE_URI);
                    iv.setScaleType(ScaleType.CENTER_CROP); 


                }catch (Exception e){
                    imageExists = false; 

                }
            }
            if (!imageExists){

                iv.setBackgroundResource(R.drawable.default_img);
                iv.setScaleType(ScaleType.CENTER_CROP); 
            }
        }
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            View view = LayoutInflater.from(context).inflate(R.layout.contacts_list, null);

            return view;

        }

    }

如果您需要更改列表视图中其他项目的背景,则可以创建多个“ list_item”布局,然后根据您的数据来填充相应的布局。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM