所以我试图掌握在Android中使用自定义控件.但我的应用程序在尝试创建活动时崩溃了.这是代码:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
这是布局xml:
<com.myApp.MyEditText
android:id="@+id/edtTaskName"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
Run Code Online (Sandbox Code Playgroud) 我试图像这样设计一个具有阴影(底部和右侧)的EditText字段

尝试谷歌搜索和搜索许多SO讨论,但都是TextView而不是EditText.
这是我的代码向输入文本添加阴影,但不向TextField添加阴影
<EditText android:id="@+id/txtpin"
android:maxLength="4"
android:layout_marginLeft="10dp"
android:layout_height="37dp"
android:gravity="center_horizontal"
android:inputType="textPassword"
android:longClickable="false"
android:layout_width="160dp"
android:shadowColor="@color/Black"
android:shadowDx="1.2"
android:shadowDy="1.2"
android:shadowRadius="1.5"
android:background="@color/White">
<requestFocus></requestFocus>
</EditText>
Run Code Online (Sandbox Code Playgroud)
我想它需要一些可绘制的自定义xml视图,但没有得到确切的想法.实现这一目标的逻辑是什么.
任何帮助,将不胜感激.
我正在开发联系人应用程序,它会添加电子邮件地址,电话号码.我必须在代码本身动态创建编辑文本.我不知道如何以及在何处实施这一逻辑,建议任何帮助都会感激不尽.
我有几个EditText字段,我想用setOnFocusChangeListener保存到SQLiteDatabase.我是否必须单独在每个上设置一个onFocusChangeListener,或者是否有某种类型的捕获?(getActivity().findViewByID因为这是一个片段)
final TextView txtName = (TextView)getActivity().findViewById(R.id.clientHeader);
final TextView txtCompany = (TextView)getActivity().findViewById(R.id.txtContactCompany);
final TextView txtPosition = (TextView)getActivity().findViewById(R.id.txtContactPosition);
txtName.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
saveThisItem(txtClientID.getText().toString(), "name", txtName.getText().toString());
}
}
});
txtCompany.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
saveThisItem(txtClientID.getText().toString(), "company", txtCompany.getText().toString());
}
}
});
txtPosition.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
saveThisItem(txtClientID.getText().toString(), "position", txtPosition.getText().toString());
}
}
});
Run Code Online (Sandbox Code Playgroud)
就像...有一些方法可以获得EditText Views的ArrayList <EditText>,为现有的editTexts指定一个指针(对不起,不确定如何)并将onFocusChangeListener设置为整个arraylist?或者,甚至,遍历ArrayList并将onFocusChangeListener设置为每个成员?
或者是一种检测任何onFocusChangeListener事件的方法,只是将所有数据保存到数据库中,无论偶然发生什么EditText?
在我的EditText领域,我想给一些min文本作为强制,最大文本作为限制,有没有办法实现呢?
如果要键入文本,则数字计数必须减少.我该怎么办?
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="24dp"
android:maxLength="175"
android:ems="10" />
Run Code Online (Sandbox Code Playgroud)
这是我添加的activity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_layout);
System.out.println(PRAYER_CATEGORY.length);
tvPrayer = (TextView) findViewById(R.id.mystate);
spinnerPrayers = (Spinner) findViewById(R.id.spinnerstate);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, PRAYER_CATEGORY);
adapter_state
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerPrayers.setAdapter(adapter_state);
value=(EditText)findViewById(R.id.editText1);
value
.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
if (value.getText().toString().trim()
.length() < 3) {
value.setError("Failed");
} else {
value.setError(null);
}
}
else {
if (value.getText().toString().trim()
.length() < 3) {
value.setError("Failed"); …Run Code Online (Sandbox Code Playgroud) 我有以下代码用于我的edittext格式,因为它可以接受任何输入我没有设置任何输入类型:
if (cardNumberEditText != null) {
cardNumberEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int currSel = cardNumberEditText.getSelectionStart();
cardNumberEditText.removeTextChangedListener(textWatcher);
.
.
cardNumberEditText.setText(formattedNumber);
.
.
cardNumberEditText.setSelection(currSel);
cardNumberEditText.addTextChangedListener(textWatcher);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
Run Code Online (Sandbox Code Playgroud)
所以最初我得到的默认输入类型是ABC,现在当我将其更改为?123(使用ABC/123?toggel按钮)时,输入一些数字后键盘会变回ABC.这个代码可以在三星设备s3和sywpe上正常工作,但不能在L和HTC上使用nexus
当我评论onTextChanged中的所有代码时,它工作正常.因此,当我调查时,我发现罪魁祸首是cardNumberEditText.setText(formattedNumber);
我没有设置任何输入类型,我只是使用键盘上的ABC /?123切换键进行切换
任何帮助/建议为什么会发生这种情况(在少数设备上),我该如何纠正?
我想创建我的编辑文本字段,而不是底部的行,就像在这张图片中一样
现在我的编辑文本看起来像这样
我该怎么做?
当我在文本中插入文本时,EditText文本与其行之间存在异常间隙EditText.这是我的终端上的一个版画屏幕,你可以看到我正在谈论的这个差距,它标有红色.

我玩弄了文字对齐和引力,但没有成功.
这是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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TableLayout
android:id="@+id/startJourneyLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/startLocationTxtView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_column="0"
android:layout_gravity="start"
android:text="@string/startLocation"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<EditText
android:id="@+id/startLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="1"
android:gravity = "bottom"
android:hint="Some text"
android:inputType="text"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_column="2"
android:src="@drawable/my_ic_location"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/endLocationTxtView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_column="0"
android:layout_gravity="start"
android:gravity="center"
android:text="@string/endLocation"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<EditText
android:id="@+id/endLocation"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_column="1"
android:inputType="text"/>
</TableRow>
<Button
android:id="@+id/goButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:text="@string/go"/>
</TableLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
有人可以发现为什么会这样,并解释我如何解决它?
---------------编辑----------------------- …
我不知道为什么但是当我尝试这样做时,我无法在EditText上输入/写入.EditText的光标不会出现,出现时键盘不是数字键盘.我有以下部分代码:
<LinearLayout 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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.android.asminhasdespesas.Meta_1Activity">
<TextView android:text="Test:"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:textColor="#000000"
android:cursorVisible="true"
android:ems="10"
android:id="@+id/editTextMeta"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:focusable="true"
android:focusableInTouchMode="true" />
<TextView
android:text="€"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow>
<Button
android:id="@+id/buttonNaoColocar"
android:text="Cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"/>
<Button
android:id="@+id/buttonOk"
android:text="Ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"/>
</TableRow>
</TableLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我尝试用"android:cursorVisible ="true"甚至"android:textColor ="#000000"来解决这种情况,我也试过"android:focusable ="true" "android:focusableInTouchMode ="true""但问题坚持,我无法在EditText上输入.
你能帮我么?
我只是在编辑文本的高度达到布局高度的末尾时,我试图在编辑文本中显示滚动条.如果我输入/添加空格,但是当我的文本到达编辑文本宽度的末尾时,它会导致问题.一旦我的文本到达我的编辑文本宽度的末尾,它就开始向我显示滚动条,而当我到达屏幕的(高度)结束时它应该显示.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editFeedback"
android:layout_width="0dp"
android:layout_height="0dp"
android:bufferType="spannable"
android:gravity="top"
android:hint="Some text"
android:inputType="textCapSentences|textMultiLine"
android:minHeight="120dp"
android:overScrollMode="ifContentScrolls"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
android:selectAllOnFocus="true"
android:textIsSelectable="true"
android:textSize="16sp"
app:layout_constraintBottom_toTopOf="@+id/buttonSend"
app:layout_constraintEnd_toEndOf="@id/guidelineEnd"
app:layout_constraintHeight_default="wrap"
app:layout_constraintStart_toStartOf="@id/guidelineStart"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintVertical_chainStyle="packed"
tools:text="" />
<Button
android:id="@+id/buttonSend"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="Send"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="@+id/barrierEnd"
app:layout_constraintEnd_toEndOf="@id/guidelineEnd"
app:layout_constraintTop_toBottomOf="@+id/editFeedback" />
<TextView
android:id="@+id/Footer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_marginBottom="100dp"
android:text="This is a footer"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guidelineStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="8dp" />
<android.support.constraint.Guideline
android:id="@+id/guidelineEnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="8dp" />
<android.support.constraint.Barrier
android:id="@+id/barrierEnd"
android:layout_width="wrap_content" …Run Code Online (Sandbox Code Playgroud) android ×10
android-edittext ×10
java ×2
android-xml ×1
dropshadow ×1
effects ×1
extend ×1
keyboard ×1
view ×1
xml ×1