无法在 edittext 中设置自定义图像
unable to set custom image in edittext
我正在尝试获得与 here 中所示相同的结果。但是没有图像被设置。此外,我必须单击每个编辑文本以键入一个数字。我希望光标在输入上一个时移动到下一个编辑文本(maxLength = 1)。 "star1" 是带星标的图像,"empty" 是没有星标的图像。如果有人能告诉我我做错了什么,那将非常有帮助。
activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="40dp" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<EditText
android:id="@+id/ET1"
android:layout_height="70dp"
android:layout_width="70dp"
android:singleLine="true"
android:maxLength="1"
android:inputType="number"
android:background="@drawable/round" >
</EditText>
<EditText
android:id="@+id/ET2"
android:layout_height="70dp"
android:layout_width="70dp"
android:singleLine="true"
android:maxLength="1"
android:inputType="number"
android:background="@drawable/round" >
</EditText>
<EditText
android:id="@+id/ET3"
android:layout_height="70dp"
android:layout_width="70dp"
android:singleLine="true"
android:maxLength="1"
android:inputType="number"
android:background="@drawable/round" >
</EditText>
<EditText
android:id="@+id/ET4"
android:layout_height="70dp"
android:layout_width="70dp"
android:singleLine="true"
android:maxLength="1"
android:inputType="number"
android:background="@drawable/round" >
</EditText>
</TableRow>
</TableLayout>
</LinearLayout>
Main Activity
public class MainActivity extends Activity {
EditText ET1, ET2, ET3, ET4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ET1 = (EditText)findViewById(R.id.ET1);
ET2 = (EditText)findViewById(R.id.ET2);
ET3 = (EditText)findViewById(R.id.ET3);
ET4 = (EditText)findViewById(R.id.ET4);
} // onCreate ends
private void handleEdittextListner(){
try {
ET1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if(ET1.getText().toString().trim().length()==1){
ET1.clearFocus();
ET2.requestFocus();
ET1.setBackgroundResource(R.drawable.star1);
}
}
});
ET2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
if(ET2.getText().toString().trim().length()==1){
ET2.clearFocus();
ET3.requestFocus();
ET2.setBackgroundResource(R.drawable.star1);
}
}
});
ET3.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
if(ET3.getText().toString().trim().length()==1){
//ET1.clearFocus();
ET3.clearFocus();
ET4.requestFocus();
ET3.setBackgroundResource(R.drawable.star1);
}
}
});
ET4.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
if(ET4.getText().toString().trim().length()==1){
ET4.setBackgroundResource(R.drawable.star1);
//Hide keyboard
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(ET4.getWindowToken(), 0);
}
}
});
this.ET2.setOnKeyListener(new View.OnKeyListener()
{
@Override
public boolean onKey(View paramView, int paramInt, KeyEvent paramKeyEvent) {
// TODO Auto-generated method stub
if ((paramKeyEvent.getAction() == KeyEvent.ACTION_DOWN)&&(paramInt == 67) && (MainActivity.this.ET2.getText().length() == 0)){
ET1.requestFocus();
ET1.setBackgroundResource(R.drawable.empty);
ET1.setText("");
}
return false;
}
});
this.ET3.setOnKeyListener(new View.OnKeyListener()
{
public boolean onKey(View paramView, int paramInt, KeyEvent paramKeyEvent)
{
if ((paramKeyEvent.getAction() == KeyEvent.ACTION_DOWN)&&(paramInt == 67) && (MainActivity.this.ET3.getText().length() == 0)){
ET2.requestFocus();
ET2.setBackgroundResource(R.drawable.empty);
ET2.setText("");
}
return false;
}
});
this.ET4.setOnKeyListener(new View.OnKeyListener()
{
public boolean onKey(View paramView, int paramInt, KeyEvent paramKeyEvent)
{
if ((paramKeyEvent.getAction() == KeyEvent.ACTION_DOWN)&&(paramInt == 67) && (MainActivity.this.ET4.getText().length() == 0)){
ET3.requestFocus();
ET3.setBackgroundResource(R.drawable.empty);
ET3.setText("");
}
return false;
}
});
} catch (Exception e) {
e.toString();
}
}
} // Activity ends
首先在您的布局 xml 文件中为您的根布局元素设置白色背景颜色,例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:orientation="vertical" >
在此之后,在所有 EditText
中为文本设置相同的颜色:
android:textColor="@android:color/white"
之后做类似的事情:
EditText ET1, ET2, ET3, ET4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ET1 = (EditText)findViewById(R.id.ET1);
ET2 = (EditText)findViewById(R.id.ET2);
ET3 = (EditText)findViewById(R.id.ET3);
ET4 = (EditText)findViewById(R.id.ET4);
ET1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(s.length() == 1){
ET1.setBackgroundResource(R.drawable.star1);
if(ET2.requestFocus()){
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
});
ET2.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(s.length() == 1){
ET2.setBackgroundResource(R.drawable.star1);
if(ET3.requestFocus()){
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
});
ET3.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(s.length() == 1){
ET3.setBackgroundResource(R.drawable.star1);
if(ET4.requestFocus()){
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
});
ET4.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(s.length() == 1){
ET3.setBackgroundResource(R.drawable.star1);
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(ET4.getWindowToken(), 0);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
});
}
编辑:
试着把你的 EditText
变成 FrameLayout
。在每个 FrameLayout
中设置 android:background="@drawable/round"
如:
<FrameLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/round" >
<EditText
android:id="@+id/ET1"
android:layout_height="70dp"
android:layout_width="70dp"
android:singleLine="true"
android:maxLength="1"
android:inputType="number">
</EditText>
</FrameLayout>
我正在尝试获得与 here 中所示相同的结果。但是没有图像被设置。此外,我必须单击每个编辑文本以键入一个数字。我希望光标在输入上一个时移动到下一个编辑文本(maxLength = 1)。 "star1" 是带星标的图像,"empty" 是没有星标的图像。如果有人能告诉我我做错了什么,那将非常有帮助。
activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="40dp" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<EditText
android:id="@+id/ET1"
android:layout_height="70dp"
android:layout_width="70dp"
android:singleLine="true"
android:maxLength="1"
android:inputType="number"
android:background="@drawable/round" >
</EditText>
<EditText
android:id="@+id/ET2"
android:layout_height="70dp"
android:layout_width="70dp"
android:singleLine="true"
android:maxLength="1"
android:inputType="number"
android:background="@drawable/round" >
</EditText>
<EditText
android:id="@+id/ET3"
android:layout_height="70dp"
android:layout_width="70dp"
android:singleLine="true"
android:maxLength="1"
android:inputType="number"
android:background="@drawable/round" >
</EditText>
<EditText
android:id="@+id/ET4"
android:layout_height="70dp"
android:layout_width="70dp"
android:singleLine="true"
android:maxLength="1"
android:inputType="number"
android:background="@drawable/round" >
</EditText>
</TableRow>
</TableLayout>
</LinearLayout>
Main Activity
public class MainActivity extends Activity {
EditText ET1, ET2, ET3, ET4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ET1 = (EditText)findViewById(R.id.ET1);
ET2 = (EditText)findViewById(R.id.ET2);
ET3 = (EditText)findViewById(R.id.ET3);
ET4 = (EditText)findViewById(R.id.ET4);
} // onCreate ends
private void handleEdittextListner(){
try {
ET1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if(ET1.getText().toString().trim().length()==1){
ET1.clearFocus();
ET2.requestFocus();
ET1.setBackgroundResource(R.drawable.star1);
}
}
});
ET2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
if(ET2.getText().toString().trim().length()==1){
ET2.clearFocus();
ET3.requestFocus();
ET2.setBackgroundResource(R.drawable.star1);
}
}
});
ET3.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
if(ET3.getText().toString().trim().length()==1){
//ET1.clearFocus();
ET3.clearFocus();
ET4.requestFocus();
ET3.setBackgroundResource(R.drawable.star1);
}
}
});
ET4.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
if(ET4.getText().toString().trim().length()==1){
ET4.setBackgroundResource(R.drawable.star1);
//Hide keyboard
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(ET4.getWindowToken(), 0);
}
}
});
this.ET2.setOnKeyListener(new View.OnKeyListener()
{
@Override
public boolean onKey(View paramView, int paramInt, KeyEvent paramKeyEvent) {
// TODO Auto-generated method stub
if ((paramKeyEvent.getAction() == KeyEvent.ACTION_DOWN)&&(paramInt == 67) && (MainActivity.this.ET2.getText().length() == 0)){
ET1.requestFocus();
ET1.setBackgroundResource(R.drawable.empty);
ET1.setText("");
}
return false;
}
});
this.ET3.setOnKeyListener(new View.OnKeyListener()
{
public boolean onKey(View paramView, int paramInt, KeyEvent paramKeyEvent)
{
if ((paramKeyEvent.getAction() == KeyEvent.ACTION_DOWN)&&(paramInt == 67) && (MainActivity.this.ET3.getText().length() == 0)){
ET2.requestFocus();
ET2.setBackgroundResource(R.drawable.empty);
ET2.setText("");
}
return false;
}
});
this.ET4.setOnKeyListener(new View.OnKeyListener()
{
public boolean onKey(View paramView, int paramInt, KeyEvent paramKeyEvent)
{
if ((paramKeyEvent.getAction() == KeyEvent.ACTION_DOWN)&&(paramInt == 67) && (MainActivity.this.ET4.getText().length() == 0)){
ET3.requestFocus();
ET3.setBackgroundResource(R.drawable.empty);
ET3.setText("");
}
return false;
}
});
} catch (Exception e) {
e.toString();
}
}
} // Activity ends
首先在您的布局 xml 文件中为您的根布局元素设置白色背景颜色,例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:orientation="vertical" >
在此之后,在所有 EditText
中为文本设置相同的颜色:
android:textColor="@android:color/white"
之后做类似的事情:
EditText ET1, ET2, ET3, ET4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ET1 = (EditText)findViewById(R.id.ET1);
ET2 = (EditText)findViewById(R.id.ET2);
ET3 = (EditText)findViewById(R.id.ET3);
ET4 = (EditText)findViewById(R.id.ET4);
ET1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(s.length() == 1){
ET1.setBackgroundResource(R.drawable.star1);
if(ET2.requestFocus()){
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
});
ET2.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(s.length() == 1){
ET2.setBackgroundResource(R.drawable.star1);
if(ET3.requestFocus()){
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
});
ET3.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(s.length() == 1){
ET3.setBackgroundResource(R.drawable.star1);
if(ET4.requestFocus()){
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
});
ET4.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if(s.length() == 1){
ET3.setBackgroundResource(R.drawable.star1);
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(ET4.getWindowToken(), 0);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
});
}
编辑:
试着把你的 EditText
变成 FrameLayout
。在每个 FrameLayout
中设置 android:background="@drawable/round"
如:
<FrameLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/round" >
<EditText
android:id="@+id/ET1"
android:layout_height="70dp"
android:layout_width="70dp"
android:singleLine="true"
android:maxLength="1"
android:inputType="number">
</EditText>
</FrameLayout>