在我的项目中,客户希望使用 Editbutton 来使用软键盘来更改EditText
. when 输入字母时可以正常工作,但是输入数字时没有反应。我的设备是 android 4.2,也不能在 android 4.0 上运行。
代码:
public ImageButton backBtn;
Button domestic_Renamebtn1;
Button domestic_Renamebtn2;
EditText domestic_EditText1;
EditText domestic_EditText2;
private InputMethodManager imm;
TextView timeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timeView = (TextView) findViewById(R.id.Hello);
domestic_Renamebtn1 = (Button) findViewById(R.id.User1_Edit1);
domestic_Renamebtn2 = (Button) findViewById(R.id.User1_Edit2);
domestic_EditText1 = (EditText) findViewById(R.id.User1_EditText1);
domestic_EditText2 = (EditText) findViewById(R.id.User1_EditText2);
domestic_EditText1.setEnabled(false);
domestic_EditText1.setFocusable(false);
domestic_EditText1.setFocusableInTouchMode(false);
domestic_EditText2.setRawInputType(InputType.TYPE_CLASS_PHONE);
domestic_EditText2.setEnabled(false);
domestic_EditText2.setFocusable(false);
domestic_EditText2.setFocusableInTouchMode(false);
domestic_EditText2.setRawInputType(InputType.TYPE_CLASS_PHONE);
imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(domestic_EditText1.getWindowToken(), 0);
imm.hideSoftInputFromWindow(domestic_EditText2.getWindowToken(), 0);
domestic_Renamebtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
domestic_EditText1.setFocusableInTouchMode(true);
domestic_EditText1.setEnabled(true);
domestic_EditText1.setFocusable(true);
domestic_EditText1.setActivated(true);
domestic_EditText1.setText("");
imm.showSoftInput(domestic_EditText1, InputMethodManager.SHOW_FORCED);
}
});
domestic_EditText1.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
domestic_EditText1.setEnabled(false);
domestic_EditText1.setFocusable(false);
domestic_EditText1.setFocusableInTouchMode(false);
domestic_EditText1.setActivated(false);
imm.hideSoftInputFromWindow(domestic_EditText1.getWindowToken(), 0);
}
});
domestic_Renamebtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
domestic_EditText2.setFocusableInTouchMode(true);
domestic_EditText2.setEnabled(true);
domestic_EditText2.setFocusable(true);
domestic_EditText2.setActivated(true);
domestic_EditText2.setText("");
imm.showSoftInput(domestic_EditText2, InputMethodManager.SHOW_FORCED);
}
});
domestic_EditText2.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
domestic_EditText2.setEnabled(false);
domestic_EditText2.setFocusable(false);
domestic_EditText2.setFocusableInTouchMode(false);
domestic_EditText2.setActivated(false);
imm.hideSoftInputFromWindow(domestic_EditText2.getWindowToken(), 0);
}
});
;
domestic_EditText1.addTextChangedListener(new TextWatcher() {
String str;
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
String s = arg0.toString();
if(s.indexOf("\n") >= 0){
domestic_EditText1.setText(str);
domestic_EditText1.setEnabled(false);
domestic_EditText1.setFocusable(false);
domestic_EditText1.setFocusableInTouchMode(false);
domestic_EditText1.setActivated(false);
imm.hideSoftInputFromWindow(domestic_EditText1.getWindowToken(), 0);
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
str = arg0.toString();
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
domestic_EditText2.addTextChangedListener(new TextWatcher() {
String str;
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
String s = arg0.toString();
if(s.indexOf("\n") >= 0){
domestic_EditText2.setText(str);
domestic_EditText2.setEnabled(false);
domestic_EditText2.setFocusable(false);
domestic_EditText2.setFocusableInTouchMode(false);
domestic_EditText2.setActivated(false);
imm.hideSoftInputFromWindow(domestic_EditText2.getWindowToken(), 0);
}
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
str = arg0.toString();
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
布局
<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:orientation="vertical"
tools:context=".MainActivity" >
<TextView android:id="@+id/Hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<EditText
android:id="@+id/User1_EditText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="Joe Bloggs"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#707070"
android:textSize="32sp" />
<Button
android:id="@+id/User1_Edit1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit1"/>
<EditText
android:id="@+id/User1_EditText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:text="****"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#707070"
android:textSize="32sp"/>
<Button
android:id="@+id/User1_Edit2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit2" />