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