1.onclick的3种用法android:gravity=“center”(居中)
Button btn = findViewById(R.id.button);
textView1 = findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView1.setText(“你好”);
}
});
2.Android Intent的详细解析以及用法(Intent的中文意思为“意图")
*显示网页
Uri uri = Uri.parse(“http://www.google.com”);
Intent it = new Intent(Intent.ACTION_VIEW,uri);
startActivity(it);
*显示地图
Uri uri = Uri.parse(“geo:38.899533,-77.036476”);
Intent it = new Intent(Intent.Action_VIEW,uri);
startActivity(it);
路径规划:
Uri uri = Uri.parse(“http://maps.google.com/maps?f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en”);
Intent it = new Intent(Intent.ACTION_VIEW,URI);
startActivity(it);
拨打电话:
调用拨号程序
Uri uri = Uri.parse(“tel:xxxxxx”);
Intent it = new Intent(Intent.ACTION_DIAL, uri);
startActivity(it);
Uri uri = Uri.parse(“tel.xxxxxx”);
Intent it =new Intent(Intent.ACTION_CALL,uri);
Intent 显式与隐式的用法
显示跳转到下一个活动。
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(“extra_data”, “dafadfadfa”); //传递数据
startActivity(intent);
下一个活动提取数据
Intent intent = getIntent();
String data = intent.getStringExtra(“extra_data”);
下一个活动返回数据
Intent intent = new Intent();
intent.putExtra(“data_return”, “Hello MainActivity”);
setResult(RESULT_OK, intent);
finish();
接收返回的数据
@Override
protected void onActivityResult(int requstCode, int resultCode, Intent data) {
switch (requstCode) { //请求码
case 1:
if (RESULT_OK == resultCode) { //处理结果码 RESULT_CANCELED
String returnResult = data.getStringExtra("data_return");
TextView textView1 = findViewById(R.id.textView);
textView1.setText(returnResult);
}
break;
default:
}
super.onActivityResult(requstCode, resultCode, data);
}