使用setVisibility()方法,EditText不会保持不可见

时间:2016-12-18 17:44:39

标签: android android-edittext

我正在创建一个列表,用户可以在editText中输入文本,editText将变为invisible,用户输入将使用setVisibility()方法转换为同一位置的textView。但是正在发生的事情是editText和textView正在切换,但在返回之前只有几分之一秒。如何让它们永久切换?

我的 customAdapter类

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

class CustomAdapter extends ArrayAdapter{

    public CustomAdapter(Context context, ArrayList choreText) {
        super(context, R.layout.custon_listview_row, choreText);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater myInflater = LayoutInflater.from(getContext());
        View customView = myInflater.inflate(R.layout.custon_listview_row, parent, false);

        String singleListItem = (String) getItem(position);
        final TextView choreTextStatic = (TextView) customView.findViewById(R.id.textView_ID);
        choreTextStatic.setVisibility(View.INVISIBLE);
        final EditText choreTextEdit = (EditText) customView.findViewById(R.id.editText_ID);
        //set user input in editText field
        choreTextEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {


            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                boolean handled = false;
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    // TODO do something
                    if (v != null) {
                        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);  

                    }

                    choreTextEdit.setVisibility(View.INVISIBLE);
                    choreTextStatic.setVisibility(View.VISIBLE);

                    handled = true;
                }
                return handled;
            }
        });

        ImageButton imageButton = (ImageButton) customView.findViewById(R.id.imageButton_ID);

        choreTextEdit.setText(singleListItem, TextView.BufferType.EDITABLE);



        imageButton.setImageResource(R.drawable.clock);



        return customView;
    }
}

1 个答案:

答案 0 :(得分:0)

在查看代码问题后,我发现:

在你的构造函数中:

private LayoutInflater inflater = null ;

public CustomAdapter(Context context, ArrayList choreText) {
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    adapterContext = context ;
}

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null) {
            convertView = inflater.inflate(R.layout.custon_listview_row,parent, false);
            //now use convertview
            }
    }

此外,如果你想在同一地点使用可设想性

choreTextEdit.setVisibility(View.GONE);
choreTextStatic.setVisibility(View.VISIBLE);

因为INVISIBLE仍会阻挡空间

在setOnEditorActionListener中正确传递true / false,否则不会调用action

 meditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {

            yourcalc();

            return true;
        }
        return false;
    }
});