======================================================
创建Menu的方式:
不通过xml文件
/**
* 创建按钮
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Quit");
menu.add(0, 1, 1, "No");
return super.onCreateOptionsMenu(menu);
}
/**
* 执行点击Menu的事件
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
finish();
break;
case 1:
Toast.makeText(getApplicationContext(), "Nothing happenned!",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
通过xml文件
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
<item
android:id="@+id/action"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/app_name"/>
<item
android:id="@+id/settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/hello_world"/>
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
finish();
break;
case R.id.action:
Toast.makeText(getApplicationContext(), "Nothing happenned!",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
================================================================
程序加载时,请稍等,ProgressDialog与线程之间的合作
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mProgressDialog = ProgressDialog.show(MainActivity.this,
"Please", "Please wait me !", true);
new Thread() {
@Override
public void run() {
try {
sleep(3000);
} catch (Exception e) {
} finally {
mProgressDialog.dismiss();
}
}
}.start();
}
});
private ProgressDialog mProgressDialog = null;
==================================================
// setContentView(R.layout.activity_main);
btn = new Button(MainActivity.this);
setContentView(btn);
setContentView方法可以加载iew,也可以加载一个layout
===========================================
样式style
1、setTheme(R.style.xxx)在setContentView方法之前。
2、清单文件中
3、xml文件中,布局
4、还有在一些放在中可以假如样式(不是很记得了)
===========================================
密码可见性与不可见
et = (EditText) findViewById(R.id.et);
cb = (CheckBox) findViewById(R.id.cb);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {// 可见
et.setTransformationMethod(HideReturnsTransformationMethod
.getInstance());
} else {// 不可见
et.setTransformationMethod(PasswordTransformationMethod
.getInstance());
}
}
});
==========================================
支持多国语言
1、values-xx
2、????
============================================
默认程序安装在SD上
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kk"
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0" >
加入android:installLocation="preferExternal"
================================================
ViewFlipper左右滑动事件
=================================================
一边输入一边显示,在EditText输入在TextView显示
et = (EditText) findViewById(R.id.et);
cb = (TextView) findViewById(R.id.cb);
et.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
cb.setText(et.getText());
return false;
}
});
重写onKey方法,键盘跟踪事件。
在manifest设置了android:installLocation="preferExternal"会出现以下状况,
Installation error: INSTALL_FAILED_CONTAINER_ERROR
Please check logcat output for more details.
Launch canceled!
将android:installLocation="preferExternal"改为android:installLocation="auto",或者删掉这条语句,即可。
========================================================
寻找主目录中的某个文件:
private EditText et = null;
private TextView tv = null;
private Button cb = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et);
cb = (Button) findViewById(R.id.cb);
tv = (TextView) findViewById(R.id.tv);
cb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tv.setText(searchFile(et.getText().toString()));
}
});
}
protected String searchFile(String string) {
String result = "";
File[] files = new File("/").listFiles();
for (File f : files) {
if (f.getName().indexOf(string) >= 0)
result = result + f.getName() + "\n";
}
if (result.equals("")) {
result = "SORRY,NO EXIST!";
}
return result;
}
=============================================
AutoCompleteTextView自动完成
创建Menu的方式:
不通过xml文件
/**
* 创建按钮
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "Quit");
menu.add(0, 1, 1, "No");
return super.onCreateOptionsMenu(menu);
}
/**
* 执行点击Menu的事件
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 0:
finish();
break;
case 1:
Toast.makeText(getApplicationContext(), "Nothing happenned!",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
通过xml文件
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
<item
android:id="@+id/action"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/app_name"/>
<item
android:id="@+id/settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/hello_world"/>
</menu>
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
finish();
break;
case R.id.action:
Toast.makeText(getApplicationContext(), "Nothing happenned!",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
================================================================
程序加载时,请稍等,ProgressDialog与线程之间的合作
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mProgressDialog = ProgressDialog.show(MainActivity.this,
"Please", "Please wait me !", true);
new Thread() {
@Override
public void run() {
try {
sleep(3000);
} catch (Exception e) {
} finally {
mProgressDialog.dismiss();
}
}
}.start();
}
});
private ProgressDialog mProgressDialog = null;
==================================================
// setContentView(R.layout.activity_main);
btn = new Button(MainActivity.this);
setContentView(btn);
setContentView方法可以加载iew,也可以加载一个layout
===========================================
样式style
1、setTheme(R.style.xxx)在setContentView方法之前。
2、清单文件中
3、xml文件中,布局
4、还有在一些放在中可以假如样式(不是很记得了)
===========================================
密码可见性与不可见
et = (EditText) findViewById(R.id.et);
cb = (CheckBox) findViewById(R.id.cb);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {// 可见
et.setTransformationMethod(HideReturnsTransformationMethod
.getInstance());
} else {// 不可见
et.setTransformationMethod(PasswordTransformationMethod
.getInstance());
}
}
});
==========================================
支持多国语言
1、values-xx
2、????
============================================
默认程序安装在SD上
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kk"
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0" >
加入android:installLocation="preferExternal"
================================================
ViewFlipper左右滑动事件
=================================================
一边输入一边显示,在EditText输入在TextView显示
et = (EditText) findViewById(R.id.et);
cb = (TextView) findViewById(R.id.cb);
et.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
cb.setText(et.getText());
return false;
}
});
重写onKey方法,键盘跟踪事件。
在manifest设置了android:installLocation="preferExternal"会出现以下状况,
Installation error: INSTALL_FAILED_CONTAINER_ERROR
Please check logcat output for more details.
Launch canceled!
将android:installLocation="preferExternal"改为android:installLocation="auto",或者删掉这条语句,即可。
========================================================
寻找主目录中的某个文件:
private EditText et = null;
private TextView tv = null;
private Button cb = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.et);
cb = (Button) findViewById(R.id.cb);
tv = (TextView) findViewById(R.id.tv);
cb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tv.setText(searchFile(et.getText().toString()));
}
});
}
protected String searchFile(String string) {
String result = "";
File[] files = new File("/").listFiles();
for (File f : files) {
if (f.getName().indexOf(string) >= 0)
result = result + f.getName() + "\n";
}
if (result.equals("")) {
result = "SORRY,NO EXIST!";
}
return result;
}
=============================================
AutoCompleteTextView自动完成