4,用intent在activity之间传递数据(两个Activity可能不是在一个应用程序中)
(1),从MainActivity向HelloActivity传递参数123
package com.example.hello;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity
{
private Button bt_a;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_a=(Button) findViewById(R.id.bt_a);
bt_a.setOnClickListener(new Bt_aListener());
}
class Bt_aListener implements View.OnClickListener
{
@Override
public void onClick(View v)
{
Intent intent=new Intent();
intent.putExtra("data1", "123");
intent.setClass(MainActivity.this, Hello1Activity.class);
MainActivity.this.startActivity(intent);
}
}
}
package com.example.hello;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.TextView;public class Hello1Activity extends AppCompatActivity { private TextView tv_b=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_hello1); Intent intent=getIntent(); String data=intent.getStringExtra("data1"); tv_b=(TextView) findViewById(R.id.tv_b); tv_b.setText(data); //tv_b.setText(R.string.HelloActivity); }}
(2)用MainActivity实现发短信功能:
package com.example.hello;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity
{
private Button bt_a;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_a=(Button) findViewById(R.id.bt_a);
bt_a.setOnClickListener(new Bt_aListener());
}
class Bt_aListener implements View.OnClickListener
{
@Override
public void onClick(View v) //必须重写这个方法
{
/* Intent intent=new Intent();
intent.putExtra("data1", "123");
intent.setClass(MainActivity.this, Hello1Activity.class);
MainActivity.this.startActivity(intent);*/
Uri uri=Uri.parse("smsto://0800000123");
Intent intent=new Intent(Intent.ACTION_SENDTO,uri );
intent.putExtra("data2", "欢迎使用!");
startActivity(intent);
}
}
}
5,activity中常见的控件;
(1),TextView:文本信息
(2),ExitText:可编辑的文本框
(3),Button:按钮
(4),meno:菜单
初始化一个菜单(当用户点击菜单按钮调用此方法)
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, 1,1 , R.string.menu_a); //add(当前组的id,菜单小块item的id,当前菜单中小块排序的id,小块显示的值)
menu.add(0, 1,2 , R.string.menu_b);
return super.onCreateOptionsMenu(menu);
}