1、点击空白处隐藏软键盘
/*
隐藏软键盘
* */
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (isShouldHideInput(v, ev)) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT\_METHOD\_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
return super.dispatchTouchEvent(ev);
}
if (getWindow().superDispatchTouchEvent(ev)) {
return true;
}
return onTouchEvent(ev);
}
public boolean isShouldHideInput(View v, MotionEvent event) {
if (v != null && (v instanceof EditText)) {
int\[\] leftTop = { 0, 0 };
v.getLocationInWindow(leftTop);
int left = leftTop\[0\];
int top = leftTop\[1\];
int bottom = top + v.getHeight();
int right = left + v.getWidth();
if (event.getX() > left && event.getX() < right
&& event.getY() > top && event.getY() < bottom) {
} else {
return true;
}
}
return false;
}
2、沉浸式状态栏
//设置沉浸式状态栏 android 4.4 及以上支持
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
3、设置导航栏
xml代码:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar1"
android:layout\_width="match\_parent"
android:layout\_height="wrap\_content"
android:layout\_alignParentLeft="true"
android:layout\_alignParentStart="true"
android:layout\_alignParentTop="true"
android:background="#5caef9"
android:minHeight="50dp"
android:gravity="center"
android:textAlignment="center"
>
</android.support.v7.widget.Toolbar>
后台代码:
//设置导航栏
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar1);//鑾峰彇椤甸潰鐨勫伐鍏锋爮
toolbar.setTitle("设置服务");
toolbar.setTitleMarginStart(370);
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setTitleMarginTop(10);
toolbar.setNavigationIcon(R.mipmap.allback);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SettingServiceActivity.this,MyselfLoginActivity.class);
startActivity(intent);
}
});
4、获取屏幕宽高和设置控件宽高
//设置输入框宽高
EditText tvservicepath = (EditText)findViewById(R.id.tvservicepath);
tvservicepath.setText(R.string.serverpath);//获取资源文件
//获取屏幕高宽
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW\_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels; // 屏幕宽度(像素)
int height = dm.heightPixels; // 屏幕高度(像素)
float density = dm.density; // 屏幕密度(0.75 / 1.0 / 1.5)
int densityDpi = dm.densityDpi; // 屏幕密度dpi(120 / 160 / 240)
// 屏幕宽度算法:屏幕宽度(像素)/屏幕密度
int screenWidth = (int) (width / density); // 屏幕宽度(dp)
int screenHeight = (int) (height / density);// 屏幕高度(dp)
ViewGroup.LayoutParams para;
para = tvservicepath.getLayoutParams();
para.height = screenHeight/2;
para.width = width-50;
tvservicepath.setLayoutParams(para);
5、在手机本地保存数据和读取(参考:https://blog.csdn.net/acm_th/article/details/50009575)
<EditText
android:id="@+id/input"
android:layout\_width="match\_parent"
android:layout\_height="wrap\_content"
android:hint="请输入" />
<Button
android:id="@+id/btn1"
android:layout\_width="match\_parent"
android:layout\_height="wrap\_content"
android:layout\_below="@+id/input"
android:text="保存数据" />
<Button
android:id="@+id/btn2"
android:layout\_width="match\_parent"
android:layout\_height="wrap\_content"
android:layout\_below="@+id/btn1"
android:text="读取数据"/>
package com.example.savedatademo;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button saveBtn = null;
private Button readBtn = null;
private EditText input = null;
private final static String FILE\_NAME = "xth.txt"; // 设置文件的名称
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE\_NO\_TITLE);
setContentView(R.layout.activity\_main);
input = (EditText) findViewById(R.id.input);
saveBtn = (Button) findViewById(R.id.btn1);
readBtn = (Button) findViewById(R.id.btn2);
saveBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String data = input.getText().toString();
save(data);
Toast.makeText(MainActivity.this, "保存数据成功!", Toast.LENGTH\_SHORT)
.show();
}
});
readBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer sb = read();
Toast.makeText(MainActivity.this, "读取数据为:" + sb,
Toast.LENGTH\_SHORT).show();
}
});
}
private StringBuffer read() {
FileInputStream in = null;
Scanner s = null;
StringBuffer sb = new StringBuffer();
try {
in = super.openFileInput(FILE\_NAME);
s = new Scanner(in);
while (s.hasNext()) {
sb.append(s.next());
}
} catch (Exception e) {
e.printStackTrace();
}
return sb;
}
private void save(String data) {
FileOutputStream out = null;
PrintStream ps = null;
try {
out = super.openFileOutput(FILE\_NAME, Activity.MODE\_APPEND);
ps = new PrintStream(out);
ps.println(data);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
ps.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
这是追加内容的,如果覆盖,改成:
out = super.openFileOutput(FILE_NAME, Activity.MODE_WORLD_WRITEABLE);
6、代码设置按钮被点击使用performClick()方法。
7、设置控件的显示与隐藏:
View缺省为可见的,在应用可以通过setVisibility 修改View的可见性,
View.VISIBLE View可见,View.INVISIBLE View不可以见,但仍然占据可见时的大小和位置。
View.GONE View不可见,且不占据空间。
手机扫一扫
移动阅读更方便
你可能感兴趣的文章