1.动态设置textView颜色
去res下的color.xml里添加,列入:
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="sb">#00ff00</color>
</resources>
在活动中通过:
textView.setBackgroundResource(R.color.sb);
2.点击电话号跳转到拨打页面
ntent dialIntent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + phoneNumber));//直接拨打电话
startActivity(dialIntent);
Intent dialIntent = new Intent(Intent.ACTION_CALL_BUTTON);//跳转到拨号界面
startActivity(dialIntent);
Intent dialIntent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" + phoneNumber));//跳转到拨号界面,同时传递电话号码
3.EditText
android:singgleline:"false"//实现单行输入
aandroid:maxlines:"1"//实现输入行数
Android:inputType:"textvisiblepassword"//显示输出内容
android:configChanges="orientation|screenSize"
android:screenOrientation="portrait"//则无论手机如何变动,拥有这个属性的activity都将是竖屏显示。
android:screenOrientation="landscape"//为横屏显示
4.隐藏ActionBar
ActionBar actionBar=getSupportActionBar();
if (actionBar!=null){
actionBar.hide();
}
4.list集合排序
在自己需要关联的类里实现:
public class People implements Comparable<People> {
*/需要的属性
*/
@Override
public int compareTo(People people) {
int i=this.getId()-people.getId();
return i;
}
}
我们会发现sort(List<T>)方法中List中的T必须实现Comparable<T>接口,然后实现 compareTo()方法,该方法的返回值0代表相等,1表示大于,-1表示小于;
在acyivity中加入:
private List<People> list=new ArrayList<>();
list.add(new People(5,"上海",23));
list.add(new People(4,"武汉",20));
list.add(new People(3,"武汉",50));
Collections.sort(list);
第二种:
private List<People> list=new ArrayList<>();
list.add(new People(5,"上海",23));
list.add(new People(4,"武汉",20));
list.add(new People(3,"武汉",50));
Collections.sort(list, new Comparator<People>() {
@Override
public int compare(People people, People t1) {
int i=people.getId()-t1.getId();
return i;
}
});
5.SharedPreferences
存入数据:
SharedPreferences users=getSharedPreferences("data",0);//第一个参数是文件名,第二个参数是只有当前的应用程序才可以对这个文件进行读写
SharedPreferences.Editor editor=users.edit();//添加数据
editor.putString("name","www");
editor.putString("URL","www.baidu");
editor.apply();//提交
读取数据:
SharedPreferences users=getSharedPreferences("data",0);
String name=users.getString("name","");
String url=users.getString("URL","");
Log.d("Main15Activity", "数据:"+name);
Log.d("Main15Activity", "网址:"+url);
表格画线
首先在res下的drawable建立两个xml文件:
ic_shape_line.xml竖线
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="2px"></size>
<solid android:color="#101110"></solid>
</shape>
ic_shape_row.xml横线
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:height="2px"></size>
<solid android:color="#090A09"></solid>
</shape>
<TableLayout
android:divider="@drawable/ic_shape_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:showDividers="beginning|middle|end">
<TableRow android:divider="@drawable/ic_shape_line"
android:showDividers="beginning|middle|end">
<TextView android:text="序号" android:layout_weight="1" android:gravity="center"/>
<TextView android:text="公交车编号" android:layout_weight="1" android:gravity="center"/>
<TextView android:text="承载人数" android:layout_weight="1" android:gravity="center"/>
</TableRow>
</TableLayout>
圆形图片:
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/circleImageView"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/tt"
app:civ_border_color="@android:color/holo_red_dark"
app:civ_border_width="5dp" />
给Textview加圆形边框:首先在res下的drawable里面建立一个xml布局文件如下内容
acc.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
android:useLevel="false">
<solid android:color="#EDECF0" />
<stroke
android:width="1dp"
android:color="#673AB7" />
<size android:width="40dp"
android:height="40dp" />
</shape>
给TextView加矩形边框步骤同上,内容如下:
border.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#43F309" />
<stroke
android:width="0.01dp"
android:color="#1B1212" />
<padding
android:bottom="1dp"
android:left="0.5dp"
android:right="0.5dp"
android:top="0dp" />
<size android:width="35dp"
android:height="35dp" />
</shape>
9.根据url渲染Imageview:
imageView=findViewById(R.id.image1);
new Thread(new Runnable() {
@Override
public void run() {
try {
URL picurl=new URL("http://pic3.nipic.com/20090520/2489240_222607039_2.jpg");
final Bitmap pngBM= BitmapFactory.decodeStream(picurl.openStream());
runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(pngBM);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
工具类
private Bitmap utile(String uri){
Bitmap bitmap=null;
try {
bitmap=BitmapFactory.decodeStream(new URL(uri).openStream());
} catch (IOException e) {
e.printStackTrace();
}
return bitmap ;
}
Textview跑马灯
android:singleLine="true" //单行显示
android:ellipsize="marquee" //跑马灯显示(动画横向移动)
android:marqueeRepeatLimit="marquee_forever"//永久滚动
android:focusable="true" //控件是否能够获取焦点
android:focusableInTouchMode="true" //是否在触摸模式下获得焦点
11.动态改变Button颜色
btn_signed.setBackgroundColor(Color.parseColor("#7CFC00"));
12.定时器
new Timer().schedule(new TimerTask() {
@Override
public void run() {
CommonUtil.runOnUIThread(new Runnable() {
@Override
public void run() {
textView.setText(i+"");
}
});
i++;
}
},5000,2000);
13.活动传值
Intent intent=new Intent(TextActivity.this,PracticeActivity.class);
intent.putExtra("data",num);
startActivity(intent);
Intent intent=getIntent();
String name= intent.getStringExtra("data");
textView.setText(name);
handler.postDelayed(runnable,3000);
}
private Handler handler=new Handler(){
@Override
public String getMessageName(Message message) {
return super.getMessageName(message);
}
};
Runnable runnable=new Runnable() {
@Override
public void run() {
Intent intent=new Intent(SplashActivity.this,MainActivity.class);
startActivity(intent);
}
};
@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacks(runnable);
}
13.记住密码
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
private EditText editTextname,editTextpassword;
private CheckBox checkBoxpw,checkBoxauto;
private Button buttonlogin;
private String userNameValue,passwordValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testlogin);
sharedPreferences=this.getSharedPreferences("data",0);
editTextname=findViewById(R.id.et_zh);
editTextpassword=findViewById(R.id.et_mima);
checkBoxpw=findViewById(R.id.cb_mima);
checkBoxauto=findViewById(R.id.cb_auto);
buttonlogin=findViewById(R.id.btn_login);
if (sharedPreferences.getBoolean("ISCHECK",false)){
checkBoxpw.setChecked(true);
editTextname.setText(sharedPreferences.getString("USERNAME",""));
editTextpassword.setText(sharedPreferences.getString("PASSWORD",""));
if (sharedPreferences.getBoolean("AUTO_ISCHECK",false)){
checkBoxauto.setChecked(true);
startActivity(new Intent(TestloginActivity.this, SplashActivity.class));
}
}
buttonlogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
userNameValue=editTextname.getText().toString();
passwordValue=editTextpassword.getText().toString();
if (userNameValue.equals("wang")&&passwordValue.equals("123")){
Toast.makeText(TestloginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
if (checkBoxpw.isChecked()){
editor=sharedPreferences.edit();
editor.putString("USERNAME",userNameValue);
editor.putString("PASSWORD",passwordValue);
editor.commit();
}
startActivity(new Intent(TestloginActivity.this,SplashActivity.class));
}else {
Toast.makeText(TestloginActivity.this,"用户名或密码错误,请重新登录", Toast.LENGTH_LONG).show();
}
}
});
checkBoxpw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBoxpw.isChecked()){
Toast.makeText(TestloginActivity.this,"记住密码已选中", Toast.LENGTH_SHORT).show();
sharedPreferences.edit().putBoolean("ISCHECK",true).commit();
}else {
Toast.makeText(TestloginActivity.this,"记住密码没有选中", Toast.LENGTH_SHORT).show();
sharedPreferences.edit().putBoolean("ISCHECK",false).commit();
}
}
});
checkBoxauto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBoxauto.isChecked()){
Toast.makeText(TestloginActivity.this, "自动登录已选中", Toast.LENGTH_SHORT).show();
sharedPreferences.edit().putBoolean("AUTO_ISCHECK",true).commit();
// boolean t=sharedPreferences.getBoolean("AUTO_ISCHECK",false);
}else {
Toast.makeText(TestloginActivity.this, "自动登录未选中", Toast.LENGTH_SHORT).show();
sharedPreferences.edit().putBoolean("AUTO_ISCHECK",false).commit();
}
}
});
}
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
private EditText editTextuser,editTextpassword;
private CheckBox checkBoxpassword,checkBoxauto;
private Button button;
private String nameValue,passwordValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testlogin);
editTextuser=findViewById(R.id.user_name);
editTextpassword=findViewById(R.id.user_password);
checkBoxpassword=findViewById(R.id.check_password);
checkBoxauto=findViewById(R.id.check_auto);
button=findViewById(R.id.button_login);
sharedPreferences=this.getSharedPreferences("data",0);
if (sharedPreferences.getBoolean("ISCHECK",false)){
checkBoxpassword.setChecked(true);
editTextuser.setText(sharedPreferences.getString("USERNAME",""));
editTextpassword.setText(sharedPreferences.getString("PASSWORD",""));
if (sharedPreferences.getBoolean("AUTO_ISCHECK",false)){
checkBoxauto.setChecked(true);
startActivity(new Intent(TestloginActivity.this, WebviewActivity.class));
}
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nameValue=editTextuser.getText().toString();
passwordValue=editTextpassword.getText().toString();
if (nameValue.equals("wang")&&passwordValue.equals("123")){
Toast.makeText(TestloginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
if (checkBoxpassword.isChecked()){
editor=sharedPreferences.edit();
editor.putString("USERNAME",nameValue);
editor.putString("PASSWORD",passwordValue);
editor.commit();
}
startActivity(new Intent(TestloginActivity.this,WebviewActivity.class));
}else {
Toast.makeText(TestloginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
}
}
});
checkBoxpassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBoxpassword.isChecked()){
Toast.makeText(TestloginActivity.this,"记住密码已经被勾选",Toast.LENGTH_SHORT).show();
sharedPreferences.edit().putBoolean("ISCHECK",true).commit();
}else {
Toast.makeText(TestloginActivity.this,"记住密码已经被勾选",Toast.LENGTH_SHORT).show();
sharedPreferences.edit().putBoolean("ISCHECK",true).commit();
}
}
});
checkBoxauto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBoxauto.isChecked()){
Toast.makeText(TestloginActivity.this,"自动登陆已经被勾选",Toast.LENGTH_SHORT).show();
sharedPreferences.edit().putBoolean("AUTO_ISCHECK",true).commit();
}else {
Toast.makeText(TestloginActivity.this,"自动登陆已经被勾选",Toast.LENGTH_SHORT).show();
sharedPreferences.edit().putBoolean("AUTO_ISCHECK",false).commit();
}
}
});
}
}
一些小点
<Switch
android:id="@+id/switch1"
android:textOn="关"
android:textOff="开"
android:checked="true"
android:textSize="20dp"
android:layout_width="70dp"
android:layout_height="wrap_content" />
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
//Todo
textView.setText("开");
}else {
//Todo
textView.setText("关");
}
}
});
public void image1(View view) {
if (m == 0) {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout3.setVisibility(View.VISIBLE);
imageView1.setRotation(90);//旋转
m = 1;
} else {
linearLayout1.setVisibility(View.GONE);
linearLayout3.setVisibility(View.GONE);
imageView1.setRotation(0);
m = 0;
}
15,webview
下面是HTML5引用css文件:
<link rel="stylesheet" href="../css/demo1.css">
Html5引用js文件
<script src="../js/firstjs.js"></script>
WebSettings webSettings = webView.getSettings();
// 设置与Js交互的权限
webSettings.setJavaScriptEnabled(true);
// 设置允许JS弹窗
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);