标签:
public class FirstActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.first_layout);
} }
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".FirstActivity" android:label="This is FirstActivity!"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name=".SecondActivity"> <intent-filter> <action android:name="com.example.activitytest.ACTION_START"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="com.example.activitytest.MY_CATEGORY"/> </intent-filter> </activity> <activity android:name=".ThirdActivity"> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="http" /> </intent-filter> </activity> </application>
public class FirstActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.first_layout); } }
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.first_layout); Button button1=(Button)findViewById(R.id.button_1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(FirstActivity.this,”You clicked Button 1”,Toast.LENGHT_SHORT).show(); } }); }
findView返回一个view对象,需要向下转型为Button对象,得到按钮的实例之后,需要调用setonclicklistener()方法为按钮注册一个监听器,点击按钮就会执行onclick中的方法。监听器的还可用作利用Intent在活动之间穿梭。
创建菜单文件---res--Menu-main.xml,而非布局文件。
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/add_item" android:title="Add"/> <item android:id="@+id/remove_item" android:title="Remove"/> </menu>
打开FirstActivity,重写oncreateoptionMenu方法,
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu);//第一个参数指定通过那个资源文件来创建菜单,第二个参数指定我们的菜单项将添加到哪个Menu对象中去。 return true; //表示允许该菜单显示出来 }菜单相应事件---onOptionsItemSelected()public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.add_item: Toast.makeText(this, "You clicked Add", Toast.LENGTH_SHORT).show(); break; case R.id.remove_item: Toast.makeText(this, "You clicked Remove", Toast.LENGTH_SHORT).show(); break; default: } return true; } }
2. 使用Intent在活动之间穿梭---思考:是否可利用该方法实现页面之间的跳转?
Button button1=(Button)findViewById(R.id.button_1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(FirstActivity.this,SecondActivity.class);//第一个参数表启动活动的上下文,第二个表示要启动的目标活动
intent.setData(Uri.parse("tel:10086")); startActivity(intent); } });
<activity android:name=".SecondActivity"> <intent-filter> <action android:name="com.example.activitytest.ACTION_START"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="com.example.activitytest.MY_CATEGORY"/> </intent-filter> </activity>
@Override public void onClick(View v) { Intent intent=new Intent("com.example.activitytest.ACTION_START") startActivity(intent); } });
@Override public void onClick(View v) { Intent intent=new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:10086")); startActivity(intent); } }); 此时需要在配置文件中配置:<activity android:name=".ThirdActivity"> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="http" /> </intent-filter> </activity>
标签:
原文地址:http://www.cnblogs.com/huangytao/p/5533636.html