1

我正在 View 类中制作自定义 UI,并在其中制作了 RemoteViews,但它不起作用。

查看课程

public class CustomeView extends View {

    Bitmap icon;
    float left=0;
    RemoteViews rs;
    int width=0,height=0;
    float def_value=0;
    boolean unlock=false;

    public CustomeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        icon=BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        rs=new RemoteViews(context.getPackageName() ,R.layout.custom_ui);

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        width = display.getWidth();
        height = display.getHeight();

        left=width/2;
        def_value=left;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        Paint a=new Paint();
        a.setTextSize(10);

        canvas.drawBitmap(icon, left, 0, null);
        canvas.drawText(String.valueOf(unlock), 0, 110, a);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
           setMeasuredDimension(width, height);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        if(event.getAction()==MotionEvent.ACTION_MOVE){
            left=event.getX()-(icon.getWidth()/2);
            isUnlock();
        }
        else if (event.getAction()==MotionEvent.ACTION_UP) {
            if(def_value!=left){
                left=def_value;
            }
        }
        invalidate();
        return true;
    }

    protected void isUnlock() {
        if(left<=20){           
            unlock=true;
            rs.setTextViewText(R.id.tvStatus, "True");
        }
        if(left>=width-20){
            unlock=true;
            rs.setTextViewText(R.id.tvStatus, "True");
        }
    }
}

custom_ui.xml 布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.helloworl.CustomeView 
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <TextView
        android:id="@+id/tvStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="154dp"
        android:text="False" />

</RelativeLayout>

RemoteViews 在 isUnlock 方法中更改 TextView 的值,但它不起作用。

4

1 回答 1

3

您不能使用带有 的自定义视图RemoteViews,因为其他进程无权访问您的 Java 代码。

于 2013-04-18T17:10:33.747 回答