1
2
3
4
<EditText
<!-- 设置密码框 -->
android:password="true"
</EditText>
1
2
CheckBox cb;
if (cb.isChecked()) { ... }
1
2
//获取文本
((EditText)findViewById(R.id.editText2)).getText().toString()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//设置按钮事件
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener( new View.OnClickListener() {

@Override
public void onClick(View v) {
// 用浏览器打开网址
Intent i = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.uprogrammer.cn"));

// 拨号
//Intent i = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse("tel:9510300000"));

startActivity(i);
}
});
1
2
3
4
5
6
7
<!-- 通过Launch或者Intent调用 -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="com.example.MyApplication.LAUNCH" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
</intent-filter>
1
2
3
4
5
6
7
8
9
10
 <!-- 监听网址的打开(Manifest) -->
<activity android:name="cn.uprogrammer.intentfilter.CustomActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="cn.uprogrammer.intentfilter.LAUNCH" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
</intent-filter>
</activity>
1
2
3
4
5
6
7
8
9
10
11
12
// 监听打开网址后(显示网址)
public class CustomActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_view);

TextView label = (TextView) findViewById(R.id.show_data);
Uri url = getIntent().getData();
label.setText(url.toString());
}
}

文本框属性详解 http://www.runoob.com/w3cnote/android-tutorial-textview.html

1
Drawable drawable = MainActivity.this.getResources().getDrawable(R.drawable.picture1);
1
2
3
4
// 格式化文本,"常量%1常量"
textView.setText(getString(R.string.section_format, 123));

<string name="section_format">Hello World %1$d</string>
1
2
// 规定ImageView最大值无效,需要加上下面这句:
android:adjustViewBounds
1
2
// 判断版本号
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// 是否拥有联系人权限,没有就尝试获取
private boolean mayRequestContacts() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return true;
}
if (checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
return true;
}
if (shouldShowRequestPermissionRationale(READ_CONTACTS)) {
Snackbar.make(mEmailView, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE)
.setAction(android.R.string.ok, new View.OnClickListener() {
@Override
@TargetApi(Build.VERSION_CODES.M)
public void onClick(View v) {
requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
}
});
} else {
requestPermissions(new String[]{READ_CONTACTS}, REQUEST_READ_CONTACTS);
}
return false;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_READ_CONTACTS) {
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
populateAutoComplete();
}
}
}
1
2
3
4
5
6
7
8
9
// 设置 Spinner 下拉菜单
String[] ctype = new String[]{"全部", "游戏", "电影", "娱乐", "图书"};
//创建一个数组适配器
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ctype);
//设置下拉列表框的下拉选项样式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

Spinner spinner = super.findViewById(R.id.spinner);
spinner.setAdapter(adapter);