文本未显示在todolist应用程序中

时间:2012-01-27 19:52:43

标签: android android-listview android-edittext textview

这是一个todolist应用程序,使每个项目看起来像在纸上 垫(侧面的边距和项目之间的黑线) 当我运行应用程序时,它不会显示输入的文本,但每次输入时都会将空项添加到列表中。此外,如果todolistitemview没有用于数组适配器构造,该应用程序工作正常。 这是我的todolistactivity文件:

package com.paad.todolist;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class TodolistActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ListView myListView = (ListView)findViewById(R.id.myListView);
        final EditText myEditText = (EditText)findViewById(R.id.myEditText);
        // Create the array list of to do items
        final ArrayList<String> todoItems = new ArrayList<String>();
        // Create the array adapter to bind the array to the listview
        int resID = R.layout.todolist_itemview;
        final ArrayAdapter<String> aa = new ArrayAdapter<String>(this, resID,
        todoItems);
        // Bind the array adapter to the listview.
        myListView.setAdapter(aa);
        myEditText.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
            if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
            {
            todoItems.add(0, myEditText.getText().toString());
            aa.notifyDataSetChanged();
            myEditText.setText("");
            return true;
            }
            return false;
            }
            });
            }
}

这是我的todolistitemview代码:

package com.paad.todolist;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;


public class todolistitemview extends TextView{
    public todolistitemview(Context context){
        super(context);
        init();
    }
    public todolistitemview(Context context,AttributeSet ats){
        super(context,ats);
        init();
    }
    public todolistitemview(Context context,AttributeSet ats,int ds){
        super(context,ats,ds);
        init();

}
    private Paint marginpaint;
    private Paint linepaint;
    private int papercolor;
    private float margin;
    private void init(){
        Resources myresources=getResources();
        marginpaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        marginpaint.setColor(myresources.getColor(R.color.notepad_margin));
        linepaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        linepaint.setColor(myresources.getColor(R.color.notepad_lines));
        papercolor=myresources.getColor(R.color.notepad_paper);
        margin=myresources.getDimension(R.dimen.notepad_margin);

    }

@Override
public void onDraw(Canvas canvas){
    canvas.drawColor(papercolor);
    canvas.drawLine(0, 0, getMeasuredWidth(), 0, linepaint);
    canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linepaint);
    canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginpaint);
    canvas.save();
    canvas.translate(margin,0);
}
}

这些是布局文件 -

main.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">
<EditText
android:id="@+id/myEditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New To Do Item"
/>
<ListView
android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

</LinearLayout>

todolist_itemview.xml -

<?xml version="1.0" encoding="utf-8"?>
<com.paad.todolist.todolistitemview
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:scrollbars="vertical"
android:textColor="@color/notepad_text"
android:fadingEdge="vertical"
/>

请帮我解决这个问题。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您没有从书中复制完整代码。在您的自定义TextView onDraw()方法中,在以下行之后:

canvas.translate(margin,0);

你必须添加:

super.onDraw(canvas);
canvas.restore();

如果没有调用超类onDraw()方法,您的自定义TextView将无法绘制最重要的部分,即文本,这就是您实际获得纸质绘图但没有文字的原因。