Android开发环境
Android Studio + Genymotion
Button事件
- 新建一个Empty项目
- 把默认的
Hello word
删掉,建立一个Button
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
- 添加事件,并增加Toast提醒
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "You chicked the button", Toast.LENGTH_SHORT).show();
}
});
}
}
- 销毁一个活动用
finish()
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
添加菜单
- 在res目录下建一个menu文件夹:
main_menu.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>
- 添加菜单事件
MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_item:
Toast.makeText(this, "You chicked Add", Toast.LENGTH_SHORT).show();
break;
case R.id.remove_item:
Toast.makeText(this, "You chicked Remove", Toast.LENGTH_SHORT).show();
break;
default:
}
return super.onOptionsItemSelected(item);
}
使用Intent
显式的Intent
- 新建一个second_layout.xml和SecondActivity.java
second_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:id="@+id/button2"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
}
}
- 在AndroidManifest.xml中为SecondActivity进行注册
<activity android:name=".SecondActivity"></activity>
- 修改MainActivity.java中的按钮点击事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
隐式的Intent
- 修改AndroidManifest.xml
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.MY_ACTION1"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.MY_CATEGORY"/>
</intent-filter>
</activity>
- 修改MainActivity.java
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.MY_ACTION1");
intent.addCategory("android.intent.category.MY_CATEGORY");
startActivity(intent);
}
});
更多的隐式Intent用法
可以在
android:scheme
用于指定数据的协议部分。android:host
用于指定数据的主机名部分。android:port
用于指定数据的端口部分,一般紧随在主机名之后。android:path
用于指定主机名和端口之后的部分,如一段网址中跟在域名之后的内容。android:mimeType
用于指定可以处理的数据类型,允许使用通配符的方式进行指定。
android:scheme
- 建立一个button
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button intentHttp = (Button) findViewById(R.id.intent1);
intentHttp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.qq.com"));
startActivity(intent);
}
});
}
- 建立一个空的layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
</RelativeLayout>
- 新建立一个活动
public class IntentActivity extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent_layout);
}
}
- 注册活动
<activity android:name=".IntentActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
</intent-filter>
</activity>
传递数据给下一个活动
- 添加button事件
Button putExtra = (Button) findViewById(R.id.putExtra);
putExtra.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data = "Hello new activity";
Intent intent = new Intent(MainActivity.this, PutextraActivity.class);
intent.putExtra("extra_data", data);
startActivity(intent);
}
});
- 新建一个layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
</RelativeLayout>
- 新建一个活动
public class PutextraActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.putextra_layout);
Intent intent = getIntent();
String data = intent.getStringExtra("extra_data");
Log.d("PutextraActivity", data);
System.out.println(">>>>>" + data);
}
}
- 注册活动
<activity android:name=".PutextraActivity"></activity>
返回数据给上一个活动
- 修改上个button的事件
Button putExtra = (Button) findViewById(R.id.putExtra);
putExtra.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String data = "Hello new activity";
Intent intent = new Intent(MainActivity.this, PutextraActivity.class);
intent.putExtra("extra_data", data);
//startActivity(intent);
startActivityForResult(intent, 1);
}
});
- 给下一个活动重写返回事件
@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putExtra("data_return", "Hello MainActivity...");
setResult(RESULT_OK, intent);
finish();
}
- 给上一个活动重写onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
String returnedData = data.getStringExtra("data_return");
Log.d("MainActivity --> ", returnedData);
}
break;
default:
}
}