Android 使用软键盘时更改编辑文本状态的代码出现问题
我已使用以下代码将editText设置为0dp的高度和宽度:Android 使用软键盘时更改编辑文本状态的代码出现问题,android,android-softkeyboard,textwatcher,Android,Android Softkeyboard,Textwatcher,我已使用以下代码将editText设置为0dp的高度和宽度: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_height="match_parent" android:lay
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context=".MainActivity">
<ListView android:id="@+id/ListView_CallData"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="1">
</ListView>
<EditText
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/editText"
android:background="@null"
android:inputType="phone">
<!--android:visibility="invisible"-->
</EditText>
</LinearLayout>
注释,但我会发布所有代码,以防它产生一些我不知道的影响
我还尝试使用getLayoutParams
,但也不起作用。谢谢你的帮助
package com.example.chris.sunil_gupta;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
//ArrayList is an implementation of List.
//ArrayList provides a resizable-array, which means that items can be added and removed from the list. An ArrayList is a
// dynamic data structure so it can be used when there is no upper bound on the number of elements, ideal for the Call
// history. From the other side, a simple Array in java is a static data structure, because the initial size of array cannot be
// changed, so it can be used only when the data has a known number of elements.
//We are making a list called listofphonehistory and we are using CallData as the datasource
private List<CallData> listofphonehistory = new ArrayList<CallData>();
// Context is an abstract class...which means what exactly?
private Context context = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
//create a ListView object called listview
ListView listview;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
//initialise the object called listview, it's a ListView object and the exact id it will appear in is called ListView_CallData
listview = (ListView) findViewById(R.id.ListView_CallData);
// call the function getCallDetails which will sort our number, name, call date, call type, duration
getCallDetails();
//CustomAdapter is the class we are going to use. We will use it to create CustomAdapter
//objects which will appear in the MainActivity activity, using the listofphonehistory
CustomAdapter adapter = new CustomAdapter(MainActivity.this, listofphonehistory);
listview.setAdapter(adapter);
// //RELEVANT CODE STARTS HERE!!!!!!!!!!!!!!!!!
// This ensures that the editText textbox will have the focus when the activity loads
// so that our soft keyboard pops up. editText is set to 0dp in width and height,
//so the user can't see it unless they need to use it. When the user starts typing
//with the keyboard then the width and height will be bigger and they can see it.
final EditText editText = (EditText) findViewById(R.id.editText);
editText.requestFocus();
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (editText.length() > 0) {
//Your query to fetch Data
// editText.getLayoutParams().width=32;
// editText.getLayoutParams().height=50;
editText.setWidth(32);
editText.setHeight(50);
}
}
});
//RELEVANT CODE ENDS HERE!!!!!!!!!!!!!!!!!
}
public void getCallDetails() {
// cursor1 gets all the items in the calllog and arranges them from newest call down
Cursor cursor1 = getContentResolver().query(
CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " DESC");
//looks like all the cell values in the calllog database are integers
int number = cursor1.getColumnIndex(CallLog.Calls.NUMBER);
int type = cursor1.getColumnIndex(CallLog.Calls.TYPE);
int date = cursor1.getColumnIndex(CallLog.Calls.DATE);
int duration = cursor1.getColumnIndex(CallLog.Calls.DURATION);
int name = cursor1.getColumnIndex(CallLog.Calls.CACHED_NAME);
//declare some new variables here; we're going to convert the integers into these
int callType;
String phoneNumber;
String callDate;
String callDuration;
Date callDateTime;
String cachedName;
while (cursor1.moveToNext()) {
// go through all the rows in the db and convert the values to strings or whatever
// It's important that these are inside the while loop. Otherwise it will try to read
// the value of a column while the cursor is at an invalid position (-1) because moveToNext()
// hasn't been called yet.
callType = cursor1.getInt(type);
phoneNumber = cursor1.getString(number);
callDate = cursor1.getString(date);
callDateTime = new Date(Long.valueOf(callDate));
callDuration = cursor1.getString(duration);
cachedName = cursor1.getString(name);
// If the contact has a name, then show the name in the calllog instead of the number
if (cachedName != null) {
phoneNumber = cachedName;
} else {
phoneNumber = phoneNumber;
}
// the string cType will give us text of either outgoing, incoming or missed calls
String cType = null;
// callType will either be 1,2 or 3
switch (callType) {
case CallLog.Calls.OUTGOING_TYPE:
cType = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
cType = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
cType = "MISSED";
break;
}
// CallData is a constructor
// We are passing the values cType, phoneNumber, callDateTime and callDuration, in the While Loop of above,
// to the CallData object and this will show us calltype, callnumber, calldatetime and callduration in our cells.
CallData calldata = new CallData(cType, phoneNumber, callDateTime, callDuration);
// add new call data info to the list, moving on down through the values in Calllog
listofphonehistory.add(calldata);
}
cursor1.close();
}
}
package com.example.chris.sunil_gupta;
导入java.util.ArrayList;
导入java.util.Date;
导入java.util.List;
导入android.app.Activity;
导入android.content.Context;
导入android.content.res.Configuration;
导入android.database.Cursor;
导入android.os.Bundle;
导入android.provider.CallLog;
导入android.text.Editable;
导入android.text.TextWatcher;
导入android.view.view;
导入android.view.inputmethod.InputMethodManager;
导入android.widget.EditText;
导入android.widget.ListView;
公共类MainActivity扩展了活动{
//ArrayList是List的一个实现。
//ArrayList提供了一个可调整大小的数组,这意味着可以从列表中添加和删除项
//动态数据结构,以便在元素数量没有上限时使用,非常适合调用
//另一方面,java中的简单数组是静态数据结构,因为数组的初始大小不能
//已更改,因此仅当数据具有已知数量的元素时才能使用。
//我们正在制作一个名为listofphonehistory的列表,并使用CallData作为数据源
private List listofphonehistory=新建ArrayList();
//上下文是一个抽象类…这意味着什么?
私有上下文=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//创建名为ListView的ListView对象
列表视图列表视图;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
上下文=这个;
//初始化名为listview的对象,它是一个listview对象,它将出现的确切id称为listview\u CallData
listview=(listview)findViewById(R.id.listview\u CallData);
//调用函数getCallDetails,它将对我们的号码、姓名、通话日期、通话类型和持续时间进行排序
getCallDetails();
//CustomAdapter是我们将要使用的类。我们将使用它创建CustomAdapter
//将使用listofphonehistory显示在MainActivity活动中的对象
CustomAdapter=新的CustomAdapter(MainActivity.this,listofphonehistory);
setAdapter(适配器);
////相关代码从这里开始!!!!!!!!!!!!!!!!!
//这样可以确保在加载活动时editText文本框具有焦点
//这样我们的软键盘就会弹出。editText的宽度和高度设置为0dp,
//因此,除非用户需要使用它,否则用户无法看到它
//有了键盘,宽度和高度都会更大,他们可以看到它。
final EditText EditText=(EditText)findViewById(R.id.EditText);
editText.requestFocus();
editText.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
}
@凌驾
公共无效后文本已更改(可编辑){
如果(editText.length()>0){
//获取数据的查询
//editText.getLayoutParams().width=32;
//editText.getLayoutParams().height=50;
editText.setWidth(32);
editText.setHeight(50);
}
}
});
//相关代码到此结束!!!!!!!!!!!!!!!!!
}
public void getCallDetails(){
//cursor1获取调用日志中的所有项,并从最新的调用向下排列它们
游标cursor1=getContentResolver().query(
CallLog.Calls.CONTENT_URI,null,null,null,CallLog.Calls.DATE+“DESC”);
//看起来calllog数据库中的所有单元格值都是整数
int number=cursor1.getColumnIndex(CallLog.Calls.number);
int type=cursor1.getColumnIndex(CallLog.Calls.type);
int date=cursor1.getColumnIndex(CallLog.Calls.date);
int duration=cursor1.getColumnIndex(CallLog.Calls.duration);
int name=cursor1.getColumnIndex(CallLog.Calls.CACHED_name);
//在这里声明一些新变量;我们将把整数转换成
int调用类型;
字符串电话号码;
字符串callDate;
字符串调用持续时间;
日期调用日期时间;
字符串cachedName;
while(cursor1.moveToNext()){
//遍历数据库中的所有行,并将值转换为字符串或其他内容
//重要的是这些都在while循环中,否则它将尝试读取
//光标位于无效位置(-1)时列的值,因为moveToNext()
//还没接到电话。
callType=cursor1.getInt(类型);
phoneNumber=cursor1.getString(数字);
callDate=cursor1.getString(日期);
callDateTime=新日期(Long.valueOf(callDate));
callDuration=cursor1.getString(持续时间);
cachedName=cursor1.getString(名称);
//如果联系人有姓名,则在通话记录中显示姓名,而不是号码
if(cachedName!=null){
phoneNumber=cachedName;
}否则{
phoneNumber=电话号码;
}
//字符串cType将为我们提供传出、传入或未接来电的文本
字符串cType=null;
//调用类型将为1、2或3
开关(呼叫类型){
案例CallLog.Calls.OUTGOING_类型:
cType=“外出”;
打破
凯斯凯尔
package com.example.chris.sunil_gupta;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends Activity {
//ArrayList is an implementation of List.
//ArrayList provides a resizable-array, which means that items can be added and removed from the list. An ArrayList is a
// dynamic data structure so it can be used when there is no upper bound on the number of elements, ideal for the Call
// history. From the other side, a simple Array in java is a static data structure, because the initial size of array cannot be
// changed, so it can be used only when the data has a known number of elements.
//We are making a list called listofphonehistory and we are using CallData as the datasource
private List<CallData> listofphonehistory = new ArrayList<CallData>();
// Context is an abstract class...which means what exactly?
private Context context = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
//create a ListView object called listview
ListView listview;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
//initialise the object called listview, it's a ListView object and the exact id it will appear in is called ListView_CallData
listview = (ListView) findViewById(R.id.ListView_CallData);
// call the function getCallDetails which will sort our number, name, call date, call type, duration
getCallDetails();
//CustomAdapter is the class we are going to use. We will use it to create CustomAdapter
//objects which will appear in the MainActivity activity, using the listofphonehistory
CustomAdapter adapter = new CustomAdapter(MainActivity.this, listofphonehistory);
listview.setAdapter(adapter);
// //RELEVANT CODE STARTS HERE!!!!!!!!!!!!!!!!!
// This ensures that the editText textbox will have the focus when the activity loads
// so that our soft keyboard pops up. editText is set to 0dp in width and height,
//so the user can't see it unless they need to use it. When the user starts typing
//with the keyboard then the width and height will be bigger and they can see it.
final EditText editText = (EditText) findViewById(R.id.editText);
editText.requestFocus();
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (editText.length() > 0) {
//Your query to fetch Data
// editText.getLayoutParams().width=32;
// editText.getLayoutParams().height=50;
editText.setWidth(32);
editText.setHeight(50);
}
}
});
//RELEVANT CODE ENDS HERE!!!!!!!!!!!!!!!!!
}
public void getCallDetails() {
// cursor1 gets all the items in the calllog and arranges them from newest call down
Cursor cursor1 = getContentResolver().query(
CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " DESC");
//looks like all the cell values in the calllog database are integers
int number = cursor1.getColumnIndex(CallLog.Calls.NUMBER);
int type = cursor1.getColumnIndex(CallLog.Calls.TYPE);
int date = cursor1.getColumnIndex(CallLog.Calls.DATE);
int duration = cursor1.getColumnIndex(CallLog.Calls.DURATION);
int name = cursor1.getColumnIndex(CallLog.Calls.CACHED_NAME);
//declare some new variables here; we're going to convert the integers into these
int callType;
String phoneNumber;
String callDate;
String callDuration;
Date callDateTime;
String cachedName;
while (cursor1.moveToNext()) {
// go through all the rows in the db and convert the values to strings or whatever
// It's important that these are inside the while loop. Otherwise it will try to read
// the value of a column while the cursor is at an invalid position (-1) because moveToNext()
// hasn't been called yet.
callType = cursor1.getInt(type);
phoneNumber = cursor1.getString(number);
callDate = cursor1.getString(date);
callDateTime = new Date(Long.valueOf(callDate));
callDuration = cursor1.getString(duration);
cachedName = cursor1.getString(name);
// If the contact has a name, then show the name in the calllog instead of the number
if (cachedName != null) {
phoneNumber = cachedName;
} else {
phoneNumber = phoneNumber;
}
// the string cType will give us text of either outgoing, incoming or missed calls
String cType = null;
// callType will either be 1,2 or 3
switch (callType) {
case CallLog.Calls.OUTGOING_TYPE:
cType = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
cType = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
cType = "MISSED";
break;
}
// CallData is a constructor
// We are passing the values cType, phoneNumber, callDateTime and callDuration, in the While Loop of above,
// to the CallData object and this will show us calltype, callnumber, calldatetime and callduration in our cells.
CallData calldata = new CallData(cType, phoneNumber, callDateTime, callDuration);
// add new call data info to the list, moving on down through the values in Calllog
listofphonehistory.add(calldata);
}
cursor1.close();
}
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
}
editText.requestLayout();
@Override
public void afterTextChanged(Editable s) {
if (editText.length() > 0) {
float density=getResources().getDisplayMetrics().density;
// I have width set to fill_parent in my xml file
editText.getLayoutParams().height =(int)(50*density);
editText.requestLayout();
}
else if (editText.length() == 0){
float density=getResources().getDisplayMetrics().density;
editText.getLayoutParams().height =(int)(0*density);
editText.requestLayout();
}
}