2016-02-27 64 views
1

时改变我EDITTEXT的状态,我有我的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

为了将来的参考,这对我工作。最重要的部分缺失的是:

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(); 
       } 
      } 
0

如何试图改变在下面的方法能见度:

@Override 
public void onTextChanged(CharSequence s, int start, int before, int count){ 

} 

只是改变了textView的知名度?如果我理解你的权利,这应该工作...祝你好运

+0

事情是我需要在活动开始后立即显示软键盘。完成的方式是将重点放在edittext上。一个对象如果不可见,就不会有焦点,如果我不希望用户在活动开始时看到edittext(使用不可见/可见的建议),就必须这样做。知道如果我能做到它在我的一段代码中? – CHarris

+0

您可以尝试在onCreate方法中设置softKey,将其设置为显示 – Honey