如何让我的Android活动使用其xml文件?

如何让我的Android活动使用其xml文件?,android,android-layout,android-activity,Android,Android Layout,Android Activity,我有一个非常简单的应用程序,它接受用户文本作为输入并返回。该应用程序似乎工作,除了我想要一个按钮下面返回的用户输入。现在它只是返回没有按钮的文本,尽管我已经将按钮添加到活动的xml文件中。我甚至在xml文件的图形视图上看到了按钮,因此我知道问题一定是找到了一种方法将xml文件与DisplayMessageActivity.java文件连接起来。下面是我的DisplayMessageActivity.java文件的一个片段,我认为我在这里做错了什么。也许我不应该调用setcontentview函数

我有一个非常简单的应用程序,它接受用户文本作为输入并返回。该应用程序似乎工作,除了我想要一个按钮下面返回的用户输入。现在它只是返回没有按钮的文本,尽管我已经将按钮添加到活动的xml文件中。我甚至在xml文件的图形视图上看到了按钮,因此我知道问题一定是找到了一种方法将xml文件与DisplayMessageActivity.java文件连接起来。下面是我的DisplayMessageActivity.java文件的一个片段,我认为我在这里做错了什么。也许我不应该调用setcontentview函数

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);      
    // Get the message from the intent
    Intent intention1=getIntent();
    final String message = intention1.getStringExtra(MainActivity.EXTRA_MESSAGE);


    // Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);

   // Set the text view as the activity layout
      textView.setText(message);
      setContentView(textView);



    }

这里根本没有
按钮
。您要做的是调用
setContentView()
然后,如果您想将
TextView
添加到
setContentView()
仅仅是
膨胀
任何你告诉它的
视图
/
布局。在这里,您告诉它只对您创建的
TextView
充气

有几种方法可以做到这一点,但最常见的方法是 将
TextView
添加到您的xml中(我们称之为
main.xml
。您可以在那里设置属性,并根据需要/需要在Java中更改它们。然后设置
布局

  TextView tv1;
  Button btn1;
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);      
    // Get the message from the intent
    Intent intention1=getIntent();
    final String message = intention1.getStringExtra(MainActivity.EXTRA_MESSAGE);          
    setContentView(R.layout.activity_display_message);  // where activity_display_message is the name of your xml file
    tv1 = (TextView) findViewById(R.id.text1); // assuming text1 is the id in xml of your TextView
    btn1 = (Button) findVieById(R.id.btn1); // assuming btn1 is the id in xml of your button
   }
 }

这里根本没有
按钮
。您要做的是调用
setContentView()
,然后如果您想将
TextView
添加到
setContentView(),您可以从中
充气
充气
您告诉它的
视图
/
布局
的任何内容。这里,您只告诉它充气
您创建的
文本视图

有几种方法可以做到这一点,但最常见的方法是 将
TextView
添加到您的xml中(我们称之为
main.xml
。您可以在那里设置属性,并根据需要/需要在Java中更改它们。然后设置
布局

  TextView tv1;
  Button btn1;
    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);      
    // Get the message from the intent
    Intent intention1=getIntent();
    final String message = intention1.getStringExtra(MainActivity.EXTRA_MESSAGE);          
    setContentView(R.layout.activity_display_message);  // where activity_display_message is the name of your xml file
    tv1 = (TextView) findViewById(R.id.text1); // assuming text1 is the id in xml of your TextView
    btn1 = (Button) findVieById(R.id.btn1); // assuming btn1 is the id in xml of your button
   }
 }

为什么要调用setContentView(TextView)?必须放大表示活动布局的完整布局文件ie

setContentView(R.layout.activity_layout).  //Inflate the layout of your activity
然后,您必须从父布局中为按钮充气,这样您就可以

Button button = (Button) findViewById(R.id.button1);  //Inflate the button that is inside 
                                                      //that layout
您的onCreate应该看起来更像这样

private Button button;

protected void onCreate(Bundle savedInstanceState){
   setContentView(R.layout.activity_layout);   //Call this  first

   button = findViewById(R.id.button_id);
   button.setOnClickListener(this);


   //Inflate whatever other buttons/views you have inside your activity here

确保在活动的同一布局文件中定义您。祝您好运

为什么要调用setContentView(TextView)?您必须将代表活动布局的完整布局文件充气

setContentView(R.layout.activity_layout).  //Inflate the layout of your activity
然后,您必须从父布局中为按钮充气,这样您就可以

Button button = (Button) findViewById(R.id.button1);  //Inflate the button that is inside 
                                                      //that layout
您的onCreate应该看起来更像这样

private Button button;

protected void onCreate(Bundle savedInstanceState){
   setContentView(R.layout.activity_layout);   //Call this  first

   button = findViewById(R.id.button_id);
   button.setOnClickListener(this);


   //Inflate whatever other buttons/views you have inside your activity here

确保您也在活动的同一布局文件中定义了您。祝您好运

您的意思是
setContentView(R.layout.activity_xml);
?能否显示您的xml布局?
您需要替换setContentView(textView)行中的textView;使用您的xml布局。R.layout.layout_文件名。然后您应该看到按钮。您的意思是
setContentView(R.layout.activity_xml);
?您可以显示您的xml布局吗?
您需要替换setContentView(textView)行中的textView;使用您的xml layout.R.layout.layout_文件名。然后您应该看到按钮。不,您的
TextView的
id
应该类似于
android:id=“@+id/text1
text1可以是任何东西,但这是您在Java代码中定义
TextView
的方式。好的,如果有帮助或者您感到困惑,请告诉我。不,您的
TextView
id
应该像
android:id>一样=“@+id/text1
text1可以是任何东西,但这就是您在Java代码中定义
TextView
的方式。好的,如果有帮助或者您感到困惑,请告诉我