Button控件
Button控件继承textview,textview继承view
创建控件,在main_activity.xml中定义
5、 在mainactivity中调用secondactivity
Intent intent = new Intent(); //创建intent
intent.setClass(MainActivity.this, SecondActivity.class);
//从main指向second
intent.putExtra("str", "intent-demo"); //传递参数 startActivity(intent); //启动intent
startActivityForResult (intent, REQUEST_CODE);//启动intent,并且在结束之后返回一个code,需要重写onActivityResult方法,接受secondactivity返回的参数
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == REQUEST_CODE)
{
if(resultCode == SecondActivity.RESULT_CODE)
{
Bundle bundle = new Bundle();
String str = bundle.getString("back");
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG);
}
}
}
6、 secondactivity获取intent,处理完毕之后返回mainactivity
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent intent = getIntent(); //获取intent
Bundle bundle = intent.getExtras(); //得到intent对象
String str = bundle.getString("str"); //提取对象里的值
secondtext = (TextView)findViewById(R.id.second);
//获取second里的textview
secondtext.setText("str"); //在textview里显示值
btn = (Button)findViewById(R.id.sbtn1); //获取second里的按键
btn.setOnClickListener(listener); //设置按键的活动
}
private OnClickListener listener = new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("back", "back-data");//传递参数
setResult(RESULT_CODE, intent);//给mainactivity返回代码
finish();
}
};
7、 如果所有的控件都显示在同一行,那么应该修改mainactivity.xml文件,或者出现如下错误
Wrong orientation? No orientation specified, and the default is horizontal, yet this layout has multiple
children where at least one has layout_width="match_parent"
修改xml文件为
"http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"