Android学习笔记函数

//调用新的Activity
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
startActivity(intent);

//读写配置文件
SharedPreferences mSharedPreferences=getSharedPreferences("config_name",MODE_PRIVATE);
xxxxx=mSharedPreferences.getString("config", "default");

mSharedPreferences.edit().putString("config", str).commit();

new AlertDialog.Builder(this).setTitle("请输入").setIcon(
android.R.drawable.ic_dialog_info).setView(
new EditText(this)).setPositiveButton("确定", null)
.setNegativeButton("取消", null).show();

弹出窗口提示
//                     new  AlertDialog.Builder(this)
//                    .setTitle("提示")
//                    .setMessage("IP或者端口不能为空")
//                    .setPositiveButton("重新输入", null)
//                    .show();

弹出小提示 几秒后自动消失
Toast.makeText(getBaseContext(), "IP或者端口不能为", Toast.LENGTH_SHORT).show();

打开网页
Uri uri = Uri.parse("https://www.baidu.com");
Intent intent2 = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent2);

//安卓SOCKET类方法
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Socket_Android {
public String doSocket(String m_ip,int m_port,String str) {
String income="";
Socket sock = null;
try {

// 应用Server的IP和端口建立Socket对象
sock = new Socket(m_ip, m_port);

PrintWriter writer = new PrintWriter(sock.getOutputStream());
writer.println(str);
writer.flush();
InputStreamReader stream_reader = new InputStreamReader(sock.getInputStream());
BufferedReader reader = new BufferedReader(stream_reader);
char[] buff = new char[1024];
reader.read(buff);
int maxcount = 0;
for (int i = 0; i < buff.length; i++) {
if (buff[i] == 0) {
maxcount = i;
break;
}
}
char[] buff1 = new char[maxcount];

for (int i = 0; i < maxcount; i++) {
buff1[i] = buff[i];
}

income = new String(buff1);

reader.close();
System.out.println(income);
sock.close();

} catch (Exception e) {
e.printStackTrace();
}
return income;
}

}

//循环执行某个函数
final Handler m_handle = new Handler();
final Runnable m_runable = new Runnable() {
@Override
public void run() {
System.out.println("tttttttttttttttttttt");
m_handle.postDelayed(m_runable, 1000);
}
};
//初次调用
m_handle.postDelayed(m_runable, 1000);
//字符串转换
Integer.valueOf(port).intValue()  String转INT

//线程更新UI
private Handler handler = new Handler();

private Runnable myRunnable= new Runnable() {
public void run() {

if (true) {
handler.postDelayed(this, 1000);
count++;
}
tvCounter.setText("Count: " + count);

}
};

调用
handler.post(myRunnable);

handler.post(myRunnable,time);
time is"1000"

//主界面手势循环
private GestureDetector detector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() {

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if ((e2.getRawX() - e1.getRawX()) > 80) {
showNext();
return true;
}

if ((e1.getRawX() - e2.getRawX()) > 80) {
showPre();
return true;
}
return super.onFling(e1, e2, velocityX, velocityY);
}

});

@Override
public boolean onTouchEvent(MotionEvent event) {
detector.onTouchEvent(event);
return super.onTouchEvent(event);
}

/**
* 当前页面索引
*/
int i = 0;

/**
* 显示下一个页面
*/
protected void showNext() {
// 三元表达式控制3个页面的循环.
tabHost.setCurrentTab(i = i == 2 ? i = 0 : ++i);
Log.i("kennet", i + "");

}

/**
* 显示前一个页面
*/
protected void showPre() {
// 三元表达式控制3个页面的循环.
tabHost.setCurrentTab(i = i == 0 ? i = 2 : --i);

}

}

//设置TabHost的高度
tabWidget = tabHost.getTabWidget();
for (int i = 0; i < tabWidget.getChildCount(); i++) {
tabWidget.getChildAt(i).getLayoutParams().height = 80;
tabWidget.getChildAt(i).getLayoutParams().width = 65;
TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(
android.R.id.title);
tv.setTextSize(15);
tv.setTextColor(this.getResources().getColorStateList(
android.R.color.white));

}

//定时跳转

Timer timer = new Timer();
TimerTask task = new TimerTask(){
@Override
public void run() {
Intent intent = new Intent(welcome.this, MainActivity.class);
startActivity(intent);
welcome.this.finish();
}
};
timer.schedule(task, 2000);

//定时更新UI
handler.post(myRunnable);

private Handler handler = new Handler();

private Runnable myRunnable= new Runnable() {
public void run() {
if (true) {
handler.postDelayed(this, 5000);
}
if (ExpirationDate!=null) {
String[] strarray = ExpirationDate.split("#");
m_ExpirationDatetextview.setText("到期时间:" + strarray[0]+"   剩余:"+strarray[1]+"天");
}

Toast.makeText(getBaseContext(), "定时更新UI", Toast.LENGTH_SHORT).show();
}
};

//异步方法
private class MyTask extends AsyncTask<String, Integer, String> {
//onPreExecute方法用于在执行后台任务前做一些UI操作
@Override
protected void onPreExecute() {

}

//doInBackground方法内部执行后台任务,不可在此方法内修改UI
@Override
protected String doInBackground(String... params) {

String str = m_SocketConnection.doSocket(m_ip,
Integer.valueOf(m_port).intValue(),
"<HTML><CMD>1007</CMD><SORT>"+params[0]+"</SORT><IP></IP><HOSTIP>192.168.1.237</HOSTIP><VERSION>2.0.3</VERSION><MD5>31b3b31a1c2f8a370206f111127c0dbd</MD5></HTML>");

cdmstr=params[0];
return str;
}

//onProgressUpdate方法用于更新进度信息
@Override
protected void onProgressUpdate(Integer... progresses) {

}

//onPostExecute方法用于在执行完后台任务后更新UI,显示结果
@Override
protected void onPostExecute(String result) {

if (result!=null) {
JSONArray arr = m_tools.GetJson(result);
strarr = new String[arr.length()];
strarr[0] = "null";
try {
for (int i = 0; i < arr.length(); i++) {
strarr[i] = arr.get(i).toString();
System.out.println("----------------" + strarr[i]);
}
} catch (JSONException e) {
e.printStackTrace();
}
AlertDlg(strarr);
}
else{
Toast.makeText(getBaseContext(), "查询异常", Toast.LENGTH_SHORT).show();
}
}

//onCancelled方法用于在取消执行中的任务时更改UI
@Override
protected void onCancelled() {

}
}

private MyTask mTask;
mTask = new MyTask();
mTask.execute("10011");

//判断数值的合法性
Pattern p = Pattern.compile("[0-9]*");//数字
Matcher m = p.matcher(inputName);
if(m.matches())

p=Pattern.compile("[a-zA-Z]");  //字母

p=Pattern.compile("[\u4e00-\u9fa5]");  //汉字

//带图片的小提示

Toast toast=Toast.makeText(getApplicationContext(), "显示带图片的toast", 5000);//显示5秒钟
toast.setGravity(Gravity.CENTER, 0, 0);
//创建图片视图对象
ImageView imageView= new ImageView(getApplicationContext());
//设置图片
//imageView.setImageResource(R.drawable.);
//获得toast的布局
LinearLayout toastView = (LinearLayout) toast.getView();
//设置此布局为横向的
toastView.setOrientation(LinearLayout.HORIZONTAL);
//将ImageView在加入到此布局中的第一个位置
toastView.addView(imageView, 0);
toast.show();

//安卓启用新线程
//加载网络动态图片
new Thread(new Runnable() {
@Override
public void run() {

}
}).start();

上一篇:AC日记——找最大数序列 openjudge 1.9 10


下一篇:在MySQL中实现Rank高级排名函数【转】