2011-10-04 37 views
7

Vì vậy, tôi đang cố gắng nắm bắt bằng cách sử dụng các điều khiển tùy chỉnh trong Android. Nhưng ứng dụng của tôi gặp sự cố khi cố gắng tạo hoạt động. Dưới đây là các mã:Mở rộng EditText trong Android. Tôi đang làm gì sai?

package com.myApp; 
import android.content.Context; 
import android.widget.EditText; 
import android.view.View; 
import android.view.View.OnClickListener; 

public class MyEditText extends EditText implements OnClickListener { 

    public MyEditText(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 
    public void FlashBorder() 
    { 
     //do some custom action 
    } 
    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     EditText txt = (EditText) v; 
     txt.selectAll(); 
    } 
} 

Đây là xml bố trí:

<com.myApp.MyEditText 
    android:id="@+id/edtTaskName" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent"/> 
+0

kiểm tra url này :: http://stackoverflow.com/questions/2026873/android-way-to-appear-bordered-text-on-the-textview –

Trả lời

9

Bạn sẽ cần phải thực hiện những cấu trúc:

public class TestEditText extends EditText { 
    public TestEditText(Context context) { 
     super(context); 
    } 

    public TestEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public TestEditText(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    public TestEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 
} 

ví dụ cố gắng làm như sau:

public TestEditText(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    Log.i("attribute name at index 0", attrs.getAttributeName(0)); 
} 

bạn sẽ nhận được sau trong logcat của bạn:

attribute name at index 0 = id 

để phân phối các thuộc tính XML này vào lớp Siêu (EditText), bạn phải ghi đè các hàm tạo này.

Hy vọng rằng Trợ giúp.

+2

Cảm ơn bạn! Tôi muốn ít nhất tôi có thể nhận được một dấu hiệu của các lỗi về trình biên dịch hoặc thời gian chạy là gì. Nhưng Eclipse chỉ dừng lại ở giữa một số cuộc gọi phương thức nội bộ mà không có bất kỳ thông tin ngoại lệ nào, không có lỗi, không có gì. – taralex

+0

Các thuộc tính xml nằm trong attribute AtributeSet vì vậy nó là cần thiết để thực hiện chúng :) –

+0

'Lỗi lạm phát lớp CustomEditText' ... không làm việc cho tôi ...: -/ –

1

Bạn phải thêm constructor này để tạo bất kỳ tùy chỉnh View.

public MyEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 
..... 
} 

thay vì

public MyEditText(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 
0
import android.annotation.TargetApi; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Typeface; 
import android.os.Build; 
import android.util.AttributeSet; 




/** 
* Created by rohann on 14/07/2016. 
*/ 
public class LightEditText extends android.widget.EditText{ 

    public LightEditText(Context context) { 
     super(context); 
     setFont(); 

    } 

    public LightEditText(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setFont(); 
    } 

    public LightEditText(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     setFont(); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public LightEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
     setFont(); 
    } 

    /** 
    * This method is used to set the given font to the TextView. 
    */ 
    private void setFont() { 
     Typeface typeface = TypefaceCache.get(getContext().getAssets(), "fonts/Roboto-Light.ttf"); 
     setTypeface(typeface); 
    } 

    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
    } 
} 
Các vấn đề liên quan