我使用以下代码将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();
}
}
答案 0 :(得分:0)
如何尝试在以下方法中更改可见性:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
}
只是更改textView
的可见性?如果我理解你的话,这应该有效......祝你好运
答案 1 :(得分:0)
为了将来参考,这对我有用。缺少的重要部分是:
editText.requestLayout();
这基本上告诉我的应用程序实现我的新布局,其中包含一个更改尺寸的edittext。当我的编辑文本中没有任何内容时,我不希望它可见。当用户输入我的编辑文本时,我希望它可见。这是我的代码:
@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();
}
}