使用软键盘时更改我的 Edittext 状态的代码有问题
trouble with code to change the state of my Edittext when using soft keyboard
我使用以下代码将我的 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: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>
我想要的是更改 editText 的大小(用户只有在开始使用 softKeyBoard 键入时才能看到它)但它始终保持不可见;即,0 高度和 0 宽度。这是我 MainAcitivity.java.
中的代码
你会发现我认为是
之间的相关代码
//RELEVANT CODE STARTS HERE!!!!!!!!!!!!!!!!!
评论,但我会发布所有代码以防它产生一些我不知道的影响。
我也尝试过使用 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();
}
}
尝试使用以下方法更改可见性如何:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
}
只是改变 textView
的可见性?如果我没理解错的话,这应该有用...祝你好运
为了将来参考,这对我有用。缺少的重要部分是:
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();
}
}
我使用以下代码将我的 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: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>
我想要的是更改 editText 的大小(用户只有在开始使用 softKeyBoard 键入时才能看到它)但它始终保持不可见;即,0 高度和 0 宽度。这是我 MainAcitivity.java.
中的代码你会发现我认为是
之间的相关代码//RELEVANT CODE STARTS HERE!!!!!!!!!!!!!!!!!
评论,但我会发布所有代码以防它产生一些我不知道的影响。
我也尝试过使用 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();
}
}
尝试使用以下方法更改可见性如何:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
}
只是改变 textView
的可见性?如果我没理解错的话,这应该有用...祝你好运
为了将来参考,这对我有用。缺少的重要部分是:
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();
}
}