Android 記事本控制項

來源:互聯網
上載者:User

  (http://wallage.blog.163.com/blog/static/17389624201071823443675/?latestBlog)

   android 可以任意繼承控制項而寫出自訂的漂亮的控制項來。要寫自訂的可以需要繼續android.widget.EditText,並且重寫其它onDraw就可以得到自己的編輯框。

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.WindowManager;
import android.widget.EditText;
public class TelEdit extends EditText{
Context mContext;
public TelEdit(Context context) {
   super(context); 
   mContext = context;
}

public TelEdit(Context context, AttributeSet attrs) {
   super(context, attrs);
   mContext = context;
}

public TelEdit(Context context, AttributeSet attrs, int defStyle) {
   super(context, attrs, defStyle);
   mContext = context;
}
protected void onDraw(Canvas canvas)
{
   WindowManager wm = (WindowManager) mContext.getSystemService("window");
   int windowWidth = wm.getDefaultDisplay().getWidth();
   int windowHeight = wm.getDefaultDisplay().getHeight();
  
   Paint paint = new Paint();
   paint.setStyle(Paint.Style.FILL);
   paint.setColor(Color.BLACK);
  
   int paddingTop    = getPaddingTop();
   int paddingBottom = getPaddingBottom();
        
   int scrollY       = getScrollY();
   int scrollX       = getScrollX()+windowWidth;
   int innerHeight   = scrollY + getHeight() - paddingTop - paddingBottom;
   int lineHeight    = getLineHeight();
   int baseLine      = scrollY + (lineHeight - (scrollY % lineHeight));
  
   int x = 8;
   while (baseLine < innerHeight) {
    //canvas.drawBitmap(line, x, baseLine + paddingTop, paint); 
    canvas.drawLine(x, baseLine + paddingTop,scrollX, baseLine + paddingTop, paint);
    baseLine += lineHeight;
   }
  
   super.onDraw(canvas);
}
}

布局檔案:

<com.xxx.tel.common.TelEdit 
android:text="@+id/EditText01" 
android:id="@+id/EditText01" 
android:layout_width="fill_parent" 
android:background="@drawable/xxxx"
android:gravity="top"
android:typeface="sans"
android:capitalize="sentences"
android:lineSpacingExtra="5dip"
android:textSize="22dip"
android:textColor="@color/wite"
android:layout_height="300dip">
</com.xxx.tel.common.TelEdit>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.