机器人:如何更改按钮单击布局? [英] android: how to change layout on button click?
问题描述
我要下$ C $下按钮点击选择布局。
View.OnClickListener处理器=新View.OnClickListener(){
公共无效的onClick(视图v){
开关(v.getId()){
案例R.id.DownloadView:
// doStuff
的setContentView(R.layout.main);
打破;
案例R.id.AppView:
// doStuff
的setContentView(R.layout.app);
打破;
}
}
};
findViewById(R.id.DownloadView).setOnClickListener(处理);
findViewById(R.id.AppView).setOnClickListener(处理);
当我点击AppView按钮,布局的变化,但是当我点击DownloadView按钮,没有任何反应。
此链接说,我要开始新的活动。
但我不知道如何使用意向的code在那里开始新的活动,将一个新的文件被添加?
编辑: 我有我的code。在新的活动:
包com.example.engagiasync;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.TextView;
公共类AppView扩展活动实现OnClickListener {
@覆盖
公共无效的onCreate(包savedInstanceState){
的setContentView(R.layout.app);
TextView的电视=(TextView中)this.findViewById(R.id.thetext);
tv.setText(应用程序查看哟\ N!?);
}
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
}
}
,但它不工作,它强制关闭时,logcat中说:
按钮btnDownload =(按钮)findViewById(R.id.DownloadView);
按钮btnApp =(按钮)findViewById(R.id.AppView);
btnDownload.setOnClickListener(处理);
btnApp.setOnClickListener(处理);
View.OnClickListener处理器=新View.OnClickListener(){
公共无效的onClick(视图v){
如果(V == btnDownload){
// doStuff
意图intentMain =新的意图(CurrentActivity.this,
SecondActivity.class);
CurrentActivity.this.startActivity(intentMain);
Log.i(内容,主布局);
}
如果(V == btnApp){
// doStuff
意图intentApp =新的意图(CurrentActivity.this,
ThirdActivity.class);
CurrentActivity.this.startActivity(intentApp);
Log.i(内容,应用程序布局);
}
}
};
注:然后你应该声明在这样的清单你所有的活动:
<活动机器人:名称=SecondActivity>< /活性GT;
<活动机器人:名称=ThirdActivity>< /活性GT;
编辑:更新这部分的code :):的
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState); //加入这一行
的setContentView(R.layout.app);
TextView的电视=(TextView中)this.findViewById(R.id.thetext);
tv.setText(应用程序查看哟\ N!?);
}
注:检查此的教程关于如何切换之间的活动。
I have to following code for selecting layout on button click.
View.OnClickListener handler = new View.OnClickListener(){
public void onClick(View v) {
switch (v.getId()) {
case R.id.DownloadView:
// doStuff
setContentView(R.layout.main);
break;
case R.id.AppView:
// doStuff
setContentView(R.layout.app);
break;
}
}
};
findViewById(R.id.DownloadView).setOnClickListener(handler);
findViewById(R.id.AppView).setOnClickListener(handler);
When I click the "AppView" button, the layout changes, but when I click the "DownloadView "button, nothing happens.
This link says that I have to start a new activity.
But I don't know how to use the code there of intent to start new activity, will a new file be added?
EDIT: I have my code on the new activity:
package com.example.engagiasync;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class AppView extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState){
setContentView(R.layout.app);
TextView tv = (TextView) this.findViewById(R.id.thetext);
tv.setText("App View yo!?\n");
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
but it does not work, it force closes, the logcat says:
Button btnDownload = (Button) findViewById(R.id.DownloadView);
Button btnApp = (Button) findViewById(R.id.AppView);
btnDownload.setOnClickListener(handler);
btnApp.setOnClickListener(handler);
View.OnClickListener handler = new View.OnClickListener(){
public void onClick(View v) {
if(v==btnDownload){
// doStuff
Intent intentMain = new Intent(CurrentActivity.this ,
SecondActivity.class);
CurrentActivity.this.startActivity(intentMain);
Log.i("Content "," Main layout ");
}
if(v==btnApp){
// doStuff
Intent intentApp = new Intent(CurrentActivity.this,
ThirdActivity.class);
CurrentActivity.this.startActivity(intentApp);
Log.i("Content "," App layout ");
}
}
};
Note : and then you should declare all your activities in the manifest like this :
<activity android:name="SecondActivity" ></activity>
<activity android:name="ThirdActivity" ></activity>
EDIT : update this part of Code :) :
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);// Add THIS LINE
setContentView(R.layout.app);
TextView tv = (TextView) this.findViewById(R.id.thetext);
tv.setText("App View yo!?\n");
}
NB : check this Tutorial About How To Switch Between Activities.
这篇关于机器人:如何更改按钮单击布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!