在android中的内联编辑文本 [英] Lined edit text in android

查看:67
本文介绍了在android中的内联编辑文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



任何想法都可以在android中创建像记事本一样的内衬编辑文本。


any idea to create a lined edit text like note pad, in android.

推荐答案

看起来很漂亮它在Android开发者网站上的教程中有所介绍。



http://developer.android.com/resources/tutorials/notepad/index.html [ ^ ]
By the looks of it it's covered in this tutorial on the android developer site.

http://developer.android.com/resources/tutorials/notepad/index.html[^]


import android.content.Context;

import android.graphics .Canvas;

导入android.graphics.Color;

导入android.graphics.Paint;

导入android.graphics.Rect;

导入android.graphics.Typeface;

导入android.util.AttributeSet;

导入android.util.Log;

import android.view.KeyEvent;



/ **

*情人节于2015年9月28日创建。

* /

公共类LinedEditText扩展android.support.v7.widget.AppCompatEditText {



private Rect rect;

private Paint paint;





public LinedEditText(Context context,AttributeSet attrs){

super(context,attrs);



rect = new Rect();

paint = new Paint();



paint.setStyle(Paint.Style.STROKE);

paint.setColor(Color.parseColor(#909090));





setTypeface(context);

}



public void setLineColor(int color){

paint.setColor(颜色);

}



@Override

protected void onDraw(Canvas canvas){

int height = getHeight();

int lineHeight = getLineHeight();

int count = height / lineHeight;



//对于滚动的长文本

if(getLineCount()>数){

count = getLineCount();

}



//画第一行

int baseline = getLineBounds(0,rect);



rect.left = getPaddingLeft() - 100;

< br $>


rect.offset(5,5);



for(int i = 0; i< count; i ++){



Log.e(基线:,+基线);





canvas.drawLine(rect.left,baseline + 40,rect.right,baseline + 40,paint);



baseline + = getLineHeight();

}

super.onDraw(画布);

}



@Override

public boolean onKeyPreIme(int keyCode,KeyEvent event){

if(event.getKeyCode()== KeyEvent.KEYCODE_BACK){

dispatchKeyEvent(event);

返回false;

}

返回super.onKeyPreIme(keyCode,event);

}



private void setTypeface(Context context) ){

this.setTypeface(Typeface.createFromAsset(context.getAssets(),avenirnextregular.ttf));

}

}
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;

/**
* Created by Valentine on 9/28/2015.
*/
public class LinedEditText extends android.support.v7.widget.AppCompatEditText {

private Rect rect;
private Paint paint;


public LinedEditText(Context context, AttributeSet attrs) {
super(context, attrs);

rect = new Rect();
paint = new Paint();

paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.parseColor("#909090"));


setTypeface(context);
}

public void setLineColor(int color) {
paint.setColor(color);
}

@Override
protected void onDraw(Canvas canvas) {
int height = getHeight();
int lineHeight = getLineHeight();
int count = height / lineHeight;

// For long text with scrolling
if (getLineCount() > count) {
count = getLineCount();
}

// Draw first line
int baseline = getLineBounds(0, rect);

rect.left = getPaddingLeft() - 100;


rect.offset(5, 5);

for (int i = 0; i < count; i++) {

Log.e("Base line : ", "" + baseline);


canvas.drawLine(rect.left, baseline + 40, rect.right, baseline + 40, paint);

baseline += getLineHeight();
}
super.onDraw(canvas);
}

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
dispatchKeyEvent(event);
return false;
}
return super.onKeyPreIme(keyCode, event);
}

private void setTypeface(Context context) {
this.setTypeface(Typeface.createFromAsset(context.getAssets(), "avenirnextregular.ttf"));
}
}


这篇关于在android中的内联编辑文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆