【问题标题】:Updating a textview in different layout xml from the main activity从主活动更新不同布局 xml 中的文本视图
【发布时间】:2012-06-23 23:33:31
【问题描述】:

我在名为log.xml 的布局文件夹中创建了一个新的.xml 文件。它只包含一个TextView

是否可以在我的主要活动的 log.xml 中的 textview 上设置文本?还是只能在使用 log.xml 作为视图的活动中设置?希望你能明白我的意思,否则就不详细了。

谢谢

【问题讨论】:

  • 你的 mainActivity 有一个名为 main.xml 的主布局,并且你已经添加了一个新的布局 log.xml ,所以你应该创建一个新的 Activity ,你当然可以改变它的值,从使用意图的第一个 Activity ( MainActivity )
  • 你打算什么时候显示 log.xml;在新 Activity 中,作为 ListView(或 Spinner 等)中的一行,作为当前 Activity 中的新视图?
  • @Sam 作为单击 main.xml 中的按钮时的新活动。

标签: android


【解决方案1】:

如果您没有在“setContentView()”上设置您正在谈论的 xml,您始终可以使用布局充气器来获取它。不过,您必须使用 addView() 将电视添加到当前布局。

LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View vi = inflater.inflate(R.layout.log, null); //log.xml is your file.
TextView tv = (TextView)vi.findViewById(R.id.tv); //get a reference to the textview on the log.xml file. 

【讨论】:

  • 正是我想要的。谢谢。
  • 嗨努诺,上述充气工作正常。我想更新特定位置的 textview 值。有什么帮助。?因为,在布局内部,我是 hvae 按钮,当单击按钮时,它会从服务器获取响应并在特定的 textview 中显示更新
【解决方案2】:

除非 log.xml 包含在当前可见的布局中,否则 findViewById() 将返回 null。

由于您想在新活动中加载 TextView 时设置其文本,因此可以在用于启动 Activity 的 Intent 中传递新的字符串。

在您的第一个活动中的相应 onClick() 中:

Intent intent = new Intent(this, Second.class);
intent.putExtra("myTextViewString", textString);
startActivity(intent);

在您的第二个 Activity 的 onCreate() 中:

setContentView(R.layout.log);

TextView textView = (TextView) findViewById(R.id.textView);
Bundle extras = getIntent().getExtras();
if(extras != null) {
    String newText = extras.getString("myTextViewString");
    if(newText != null) {
        textView.setText(newText);
    }
}

【讨论】:

    【解决方案3】:

    以下解决方案对我有用 -

    1. 获取布局 XML 文件的视图对象(例如 toast_loading_data)-

      View layout = inflater.inflate(R.layout.toast_loading_data,
              (ViewGroup) findViewById(R.id.toast_layout_root));
      
    2. 从此视图中获取 TextView 元素(例如 TextView id - toast_text)-

      TextView tvToast = (TextView) layout.findViewById(R.id.toast_text);
      
    3. 设置TextView的文本-

      tvToast.setText("Loading data for " + strDate + " ...");
      
    4. 下面是来自 Main Activity 的自定义 Toast 消息的 sn-p -

      View layout = inflater.inflate(R.layout.toast_loading_data,
              (ViewGroup) findViewById(R.id.toast_layout_root));
      TextView tvToast = (TextView) layout.findViewById(R.id.toast_text);
      tvToast.setText("Loading data for " + strDate + " ...");
      Toast toast = new Toast(getApplicationContext());
      toast.setGravity(Gravity.CENTER, 0, 0); //Set toast gravity to bottom
      toast.setDuration(Toast.LENGTH_LONG);   //Set toast duration
      toast.setView(layout);  //Set the custom layout to Toast
      

    希望对你有帮助

    【讨论】:

      【解决方案4】:

      我想我明白你想说什么。如果你这样做:

      TextView tv = (TextView) findViewById(R.id.textView2);    
      tv.setText(output);
      

      其中textView2 是您要为其设置文本的文本视图的ID,您可以使用setText() 函数将其设置为任何字符串值。希望这会有所帮助!

      【讨论】:

      • 如果log.xml当前没有显示,tv将为空。
      • 他能做到吗? TextViewActivity.findViewById(R.id.textView) 其中TextViewActivity 是带有文本视图的活动?
      • 我不这么认为,findViewById() 只适用于当前的 Activity 或膨胀的 View。
      • 这个答案不正确。如果您要查找的“id”在不同的活动中,则 findViewById() 不起作用。请参考@nuno-gonçalves 正确答案。
      猜你喜欢
      • 2013-01-29
      • 2020-06-17
      • 2016-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-15
      • 1970-01-01
      相关资源
      最近更新 更多