android开发学习——day4
自己手动创建空活动,创建和加载布局,效果:界面中出现靠上对齐的button
在活动中使用Toast,效果:对点击按钮做出响应
在活动中使用menu,效果:界面中出现菜单,并且点击对应选项会有响应
Demo:
FirstActivity.java
package com.example.hs769.activitytest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.Toast; public class FirstActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first_layout); Button button1=(Button)findViewById(R.id.button_1); button1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ Toast.makeText(FirstActivity.this,"You clicked Button 1", Toast.LENGTH_SHORT).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main,menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch(item.getItemId()){ case R.id.add_item: Toast.makeText(this,"You click Add",Toast.LENGTH_SHORT).show(); break; case R.id.remove_item: Toast.makeText(this,"you click Remove",Toast.LENGTH_SHORT).show(); break; default: } return true; } }
其中重写了两个方法
first_layout.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"> <Button android:id="@+id/button_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button1" /> </LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?> <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>
Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.hs769.activitytest"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" 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> </application> </manifest>
无情的摸鱼机器