【问题标题】:Creating a scrollable textView创建一个可滚动的文本视图
【发布时间】:2014-06-30 16:35:29
【问题描述】:

我正在尝试创建一个带有可滚动文本的片段。我有以下使用以下两种方法扩展 Fragment 的类。我收到错误消息“未定义类型 exampleFragmentText 的方法 findViewbyID(int)”和未定义类型 exampleFragmentText 的方法“stSelected(boolean)”我应该怎么做?

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

        return inflater.inflate(R.layout.textview_main, container, false); //just return the  view ;

    }

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        //here you can initialise your variables,listeners,e.t.c ;
        super.onActivityCreated(savedInstanceState);

        TextView textView = (TextView) findViewbyID(R.id.myText);
        textView.stSelected(true);

     //   addListenerOnButton();
    }
protected View findViewById(int id)
{
    return getView().findViewById(id);
}

【问题讨论】:

  • 你想要 findViewById() 做什么?

标签: android textview android-scroll


【解决方案1】:

试试:

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


View view =  inflater.inflate(R.layout.textview_main, container, false);

TextView textView = (TextView) view.findViewbyID(R.id.myText);
textView.stSelected(true);
return view

}

【讨论】:

    【解决方案2】:

    替换

    textView.stSelected(true);

    textView.setSelected(true);

    另外,您不需要覆盖 findViewById()。

    【讨论】:

      【解决方案3】:

      也许你可以试试:

      TextView textView = (TextView) getView().findViewbyID(R.id.myText);
      

      这是因为,片段是基础活动的一部分。您需要查看 base 的视图才能知道组件的 id。

      我不知道我解释得好不好。对不起我的英语。

      另一个选择是,如果你的片段是这样开始的:

      @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
              View v = inflater.inflate(R.layout.fragment_new_record, container, false);
      

      您可以使用 v 来获取组件的视图,例如:

      TextView textView = (TextView) v.findViewbyID(R.id.myText)
      

      【讨论】:

      • 尝试删除这个你覆盖受保护视图的方法 findViewById(int id);
      猜你喜欢
      • 2014-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多