当我按下时,只有文本视图松开了它的值

时间:2016-12-06 11:53:37

标签: android textview fragment

我已经制作了这段代码:

public class THGSFragment extends Fragment
{
PageFragmentListener listener;
EditText tcET, tasET,wET,vET;
TextView tcTW, tasTW,vwTW, tasUM, wvUM, insert11, text;
ImageView button;

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{

    View rootView = inflater.inflate(R.layout.fragment_thg, container, false);

    View.OnClickListener handler = new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            if (view.getId() == R.id.calcEAS)
            {
             listener.replace(0,THGSCalcFragment.class,null);
            }
            else if(view.getId() == R.id.rhoUM)
            {
                RecyclerSpeedFragment dialog = new RecyclerSpeedFragment();
                dialog.setTargetFragment(THGSFragment.this, 1);
                dialog.show(getActivity().getSupportFragmentManager(), "SPEEDDIALOGTAG");

            }
        }
    };


    button= (ImageView) rootView.findViewById(R.id.calcEAS);
    button.setOnClickListener(handler);
    tcET = (EditText)rootView.findViewById(R.id.paisaET);
    tcET.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"fonts/Avenir-Book.ttf"));
    tcTW = (TextView)rootView.findViewById(R.id.tasTW);
    tcTW.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"fonts/Avenir-Book.ttf"));
    tasET = (EditText)rootView.findViewById(R.id.oatdET);
    tasET.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"fonts/Avenir-Book.ttf"));
    tasTW = (TextView)rootView.findViewById(R.id.paisaTW);
    tasTW.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"fonts/Avenir-Book.ttf"));
    wET = (EditText)rootView.findViewById(R.id.wET);
    wET.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"fonts/Avenir-Book.ttf"));
    vET = (EditText)rootView.findViewById(R.id.vET);
    vET.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"fonts/Avenir-Book.ttf"));
    tasUM = (TextView)rootView.findViewById(R.id.rhoUM);
    tasUM.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"fonts/Avenir-Medium.ttf"),Typeface.BOLD);
    tasUM.setOnClickListener(handler);
    vwTW = (TextView)rootView.findViewById(R.id.wTW);
    vwTW.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"fonts/Avenir-Book.ttf"));
    wvUM = (TextView)rootView.findViewById(R.id.vUM);
    wvUM.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"fonts/Avenir-Medium.ttf"),Typeface.BOLD);
    insert11 = (TextView)rootView.findViewById(R.id.insert11);
    insert11.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"fonts/Avenir-Book.ttf"));
    text = (TextView)rootView.findViewById(R.id.eascalculate);
    text.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"fonts/Avenir-Book.ttf"));


    return rootView;
}

public void onAttach(Context context)
{
    super.onAttach(context);

    if (context instanceof PageFragmentListener)
    {
        listener = (PageFragmentListener) context;
    } else
    {
        throw new RuntimeException(context.toString()
                + " deve implementare PageFragmentListener");
    }
}

@Override
public void onDetach()
{

    Log.d("str","Ondetach");
    super.onDetach();
    listener = null;
}

public void setText(String text)
{
 tasUM.setText(text);
}
}

运作良好。当我点击button我改变片段时,我就到了这里:

public class THGSCalcFragment extends Fragment
{

PageFragmentListener listener;
Button back, graphics;

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.fragment_thgscalc, container, false);

    Bundle bundle = getArguments();
    RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerthgs);
    recyclerView.setNestedScrollingEnabled(false);

    back = (Button)rootView.findViewById(R.id.buttonBack);
    graphics = (Button)rootView.findViewById(R.id.buttonGraphics);

    back.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"fonts/Avenir-Book.ttf"));
    graphics.setTypeface(Typeface.createFromAsset(getActivity().getAssets(),"fonts/Avenir-Book.ttf"));


    THGSAdapter adapter = new THGSAdapter(getActivity());
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
    recyclerView.setAdapter(adapter);



    return rootView;
}

public void onResume()
{
    super.onResume();

    if(getView() == null)
    {
        return;
    }

    getView().setFocusableInTouchMode(true);
    getView().requestFocus();
    getView().setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK)
            {

                listener.back();
                Log.e("resume","resume THGS");
                return true;
            }
            return false;
        }
    });
}

public void onAttach(Context context)
{
    super.onAttach(context);

    if (context instanceof PageFragmentListener)
    {
        listener = (PageFragmentListener) context;
    } else
    {
       throw new RuntimeException(context.toString()
                + " deve implementare PageFragmentListener");
    }
}

@Override
public void onDetach()
{
    super.onDetach();
    listener = null;
}
}

当我按回来时,我可以看到之前插入的EditTexts上的值但是我看不到在使用dialogFragment之前插入的textview tasUM中的值。为什么会这样?

由于

1 个答案:

答案 0 :(得分:0)

在xml中为android:freezesText使用此attr:TextView

来自文档:

  

如果设置,除了元数据(如当前光标位置)之外,文本视图还将在其冻结冰柱内包含其当前完整文本。默认情况下,这是禁用的;当文本视图的内容未存储在诸如内容提供者之类的持久位置时,它可能是有用的。对于EditText,无论属性的值如何,它始终都会启用。   必须是布尔值,“true”或“false”。