setContentView() 不起作用
这是我的代码:
public class MyActivity extends Activity implements View.OnClickListener {
private Button mHorizontalButton;
private Button mVerticalButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHorizontalButton = (Button) findViewById(R.id.horizontal_button);
mVerticalButton = (Button) findViewById(R.id.vertical_button);
mHorizontalButton.setOnClickListener(this);
mVerticalButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.horizontal_button:
setContentView(R.layout.horizontal);
break;
case R.id.vertical_button:
setContentView(R.layout.main);
break;
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This layout is vertical." />
<Button
android:text="Click for a horizontal layout"
android:id="@+id/horizontal_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
horizontal.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This layout is horizontal." />
<Button
android:text="Click for a vertical layout"
android:id="@+id/vertical_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
假设,通过单击按钮我将获得水平视图,然后再次单击后 - 垂直视图。
但点击后什么也没有发生。可能是什么问题?
Here's my code:
public class MyActivity extends Activity implements View.OnClickListener {
private Button mHorizontalButton;
private Button mVerticalButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHorizontalButton = (Button) findViewById(R.id.horizontal_button);
mVerticalButton = (Button) findViewById(R.id.vertical_button);
mHorizontalButton.setOnClickListener(this);
mVerticalButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.horizontal_button:
setContentView(R.layout.horizontal);
break;
case R.id.vertical_button:
setContentView(R.layout.main);
break;
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This layout is vertical." />
<Button
android:text="Click for a horizontal layout"
android:id="@+id/horizontal_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
horizontal.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This layout is horizontal." />
<Button
android:text="Click for a vertical layout"
android:id="@+id/vertical_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
It is supposed, that by clicking on button I'll get the horizontal view, and then after clicking once again - vertical.
But after clicking nothing happens. What might be the issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
像这样比较 v.getId() == R.id.mHorizontalButton。因为我们不能使用 == 来比较视图。
接下来的更正是将 onclickListener 设置为按钮
mVerticalButton = (Button) findViewById(R.id.vertical_button);
mVerticalButton.setOnclickListener(this)。
现在它会起作用了。
compare like this v.getId() == R.id.mHorizontalButton.as we can not compare view using ==.
Next correction is set onclickListener to button
mVerticalButton = (Button) findViewById(R.id.vertical_button);
mVerticalButton.setOnclickListener(this).
Now it will work.
您必须按照其他人的建议调用 setOnClickListener 或
在布局中设置 onclick XML 属性。
像这样:
在你的代码中:
You must either call setOnClickListener, as suggested by others or
set the onclick XML attribute in your layout.
Like this:
in your code:
试试这个
Try this
您必须使用以下代码,而不是
if (v == mHorizontalButton)
和else if (v == mVerticalButton)
:Instead of
if (v == mHorizontalButton)
andelse if (v == mVerticalButton)
, you must use:这是可行的解决方案:
在
xml
中:Here's the working solution:
And in
xml
: