当前位置: 编程技术>移动开发
本页文章导读:
▪通用方法解决UITextFiled输入时键盘遮挡有关问题 通用方法解决UITextFiled输入时键盘遮挡问题
http://www.codeios.com/thread-9770-1-1.html
我们在用键盘录入的时候,有可能会遮挡录入框,所以我们应调整UIView的位置,使其不被遮挡。我写了一个通用.........
▪ 完善实现自定义软键盘 完美实现自定义软键盘
原文
我相信是懒人推动了世界的发展,既然iphone有了自己的软件盘,我们什么还要自己实现其功能呢。so,只要寄生在上面就行了。通过这篇文章给的灵感:为UIKeybo.........
▪ 版本检测跟更新(转) 版本检测和更新(转)
版本检测和更新是每个应用程序必须做的一个功能,所以在这里晒下:
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net..........
[1]通用方法解决UITextFiled输入时键盘遮挡有关问题
来源: 互联网 发布时间: 2014-02-18
通用方法解决UITextFiled输入时键盘遮挡问题
- (void)moveView:(UITextField *)textField leaveView:(BOOL)leave
{
UIView *accessoryView = textField.inputAccessoryView;
UIView *inputview = textField.inputView;
int textFieldY = 0;
int accessoryY = 0;
if (accessoryView && inputview)
{
CGRect accessoryRect = accessoryView.frame;
CGRect inputViewRect = inputview.frame;
accessoryY = 480 - (accessoryRect.size.height + inputViewRect.size.height);
}
else if (accessoryView)
{
CGRect accessoryRect = accessoryView.frame;
accessoryY = 480 - (accessoryRect.size.height + 216);
}
else if (inputview)
{
CGRect inputViewRect = inputview.frame;
accessoryY = 480 -inputViewRect.size.height;
}
else
{
accessoryY = 264; //480 - 216;
}
CGRect textFieldRect = textField.frame;
textFieldY = textFieldRect.origin.y + textFieldRect.size.height + 20;
int offsetY = textFieldY - accessoryY;
if (!leave && offsetY > 0)
{
int y_offset = -5;
y_offset += -offsetY;
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += y_offset;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
else
{
CGRect viewFrame = CGRectMake(0, 20, 320, 460);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
}
复制代码
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self moveView:textField leaveView:NO];
}
- (void)textFieldDidEndEditing:(UITextField *)textField;
{
[self moveView:textField leaveView:YES];
}
复制代码
http://www.codeios.com/thread-9770-1-1.html
我们在用键盘录入的时候,有可能会遮挡录入框,所以我们应调整UIView的位置,使其不被遮挡。我写了一个通用的方法可以解决这个问题:
用法很简单,在UITextFieldDelegate的两个方法里分别调用一下这个方法就OK了,如下示例:
[2] 完善实现自定义软键盘
来源: 互联网 发布时间: 2014-02-18
完美实现自定义软键盘
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
复制代码
核心思路代码:
- (void)keyboardWillShow:(NSNotification *)note
{
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];//知识点
for(int i=0; i<[tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];
if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
{
[keyboard setFrame:CGRectMake(0, 460, 320, 345)];
[self congfigKeypad];
[keyboard addSubview:keyPadView1];
}
}
}
复制代码
比如配置方法可以是这样:
-(void)congfigKeypad
{
SearBtn *one = [[SearBtn alloc] initWithFrame:CGRectMake(81, 3, kNumPadW, kNumPadH) index:1 ContextString:@"1" type:kNumPadType];
[one setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
[one addTarget:self action:@selector(buttonClickAtIndex:) forControlEvents:UIControlEventTouchUpInside];
//......略
}
复制代码
添加NSMutalbeString作为文本域字串的容器,点击button后append的button对应的字串。
- (void)buttonClickAtIndex:(id)sender
{
SearBtn *btnItem = (SearBtn*)sender;
NSString *str = btnItem->btnText;
[s_text appendString:str];
[sBar setText:s_text];
}
复制代码
再实现一个deleteChar的方法作为退格键
思路:
if ([s_text length] > 0)
{
NSRange rang;
rang.location = [s_text length] - 1;
rang.length = 1;
[s_text deleteCharactersInRange:rang];
}
复制代码
现在点击各种文本域,应该就可以现实自己的键盘了。
继续优化
用textfield的代理方法控制键盘的字串类型,长度,和响应消失:
原文
我相信是懒人推动了世界的发展,既然iphone有了自己的软件盘,我们什么还要自己实现其功能呢。
so,只要寄生在上面就行了。
通过这篇文章给的灵感:
为UIKeyboardTypeNumberPad增加自定义按键
http://www.codeios.com/thread-805-1-1.html
思路:
1.用静态方法找到应用程序当前view(window)中的UIKeyboard的view
2.在键盘的view上帖上自己的view,(精彩了,这个自己的view就是你自己键盘,任意发挥,什么类型键盘都可以做了)
3.根据需要调整系统键盘的大小以满足你想要的尺寸
4.给自己的键盘view上的button添加方法,实现功能
主要代码:
添加自身类为键盘事件的观察者:
思路:
继续优化
用textfield的代理方法控制键盘的字串类型,长度,和响应消失:
[3] 版本检测跟更新(转)
来源: 互联网 发布时间: 2014-02-18
版本检测和更新(转)
版本检测和更新是每个应用程序必须做的一个功能,所以在这里晒下:
版本检测和更新是每个应用程序必须做的一个功能,所以在这里晒下:
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.util.Log;
import android.webkit.URLUtil;
import com.autoupdate.R;
/**
* 版本检测,自动更新
*
* @author shenyj-ydrh 1.通过Url检测更新 2.下载并安装更新 3.删除临时路径
*
*/
public class MyAutoUpdate {
// 调用更新的Activity
public Activity activity = null;
// 当前版本号
public int versionCode = 0;
// 当前版本名称
public String versionName = "";
// 控制台信息标识
private static final String TAG = "AutoUpdate";
// 文件当前路径
private String currentFilePath = "";
// 安装包文件临时路径
private String currentTempFilePath = "";
// 获得文件扩展名字符串
private String fileEx = "";
// 获得文件名字符串
private String fileNa = "";
// 服务器地址
private String strURL = "http://127.0.0.1:8080/ApiDemos.apk";
private ProgressDialog dialog;
/**
* 构造方法,获得当前版本信息
*
* @param activity
*/
public MyAutoUpdate(Activity activity) {
this.activity = activity;
// 获得当前版本
getCurrentVersion();
}
/**
* 检测更新
*/
public void check() {
// 检测网络
if (isNetworkAvailable(this.activity) == false) {
return;
}
// 如果网络可用,检测到新版本
if (true) {
// 弹出对话框,选择是否需要更新版本
showUpdateDialog();
}
}
/**
* 检测是否有可用网络
*
* @param context
* @return 网络连接状态
*/
public static boolean isNetworkAvailable(Context context) {
try {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
// 获取网络信息
NetworkInfo info = cm.getActiveNetworkInfo();
// 返回检测的网络状态
return (info != null && info.isConnected());
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 弹出对话框,选择是否需要更新版本
*/
public void showUpdateDialog() {
@SuppressWarnings("unused")
AlertDialog alert = new AlertDialog.Builder(this.activity)
.setTitle("新版本").setIcon(R.drawable.ic_launcher)
.setMessage("是否更新?")
.setPositiveButton("是", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 通过地址下载文件
downloadTheFile(strURL);
// 显示更新状态,进度条
showWaitDialog();
}
})
.setNegativeButton("否", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
/**
* 显示更新状态,进度条
*/
public void showWaitDialog() {
dialog = new ProgressDialog(activity);
dialog.setMessage("正在更新,请稍候...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show();
}
/**
* 获得当前版本信息
*/
public void getCurrentVersion() {
try {
// 获取应用包信息
PackageInfo info = activity.getPackageManager().getPackageInfo(
activity.getPackageName(), 0);
this.versionCode = info.versionCode;
this.versionName = info.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
/**
* 截取文件名称并执行下载
*
* @param strPath
*/
private void downloadTheFile(final String strPath) {
// 获得文件文件扩展名字符串
fileEx = strURL.substring(strURL.lastIndexOf(".") + 1, strURL.length())
.toLowerCase();
// 获得文件文件名字符串
fileNa = strURL.substring(strURL.lastIndexOf("/") + 1,
strURL.lastIndexOf("."));
try {
if (strPath.equals(currentFilePath)) {
doDownloadTheFile(strPath);
}
currentFilePath = strPath;
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
// 执行下载
doDownloadTheFile(strPath);
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 执行新版本进行下载,并安装
*
* @param strPath
* @throws Exception
*/
private void doDownloadTheFile(String strPath) throws Exception {
Log.i(TAG, "getDataSource()");
// 判断strPath是否为网络地址
if (!URLUtil.isNetworkUrl(/blog_article/strPath/index.html)) {
Log.i(TAG, "服务器地址错误!");
} else {
URL myURL = new URL(strPath);
URLConnection conn = myURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
if (is == null) {
throw new RuntimeException("stream is null");
}
//生成一个临时文件
File myTempFile = File.createTempFile(fileNa, "." + fileEx);
// 安装包文件临时路径
currentTempFilePath = myTempFile.getAbsolutePath();
FileOutputStream fos = new FileOutputStream(myTempFile);
byte buf[] = new byte[128];
do {
int numread = is.read(buf);
if (numread <= 0) {
break;
}
fos.write(buf, 0, numread);
} while (true);
Log.i(TAG, "getDataSource() Download ok...");
dialog.cancel();
dialog.dismiss();
// 打开文件
openFile(myTempFile);
try {
is.close();
} catch (Exception ex) {
Log.e(TAG, "getDataSource() error: " + ex.getMessage(), ex);
}
}
}
/**
* 打开文件进行安装
*
* @param f
*/
private void openFile(File f) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
// 获得下载好的文件类型
String type = getMIMEType(f);
// 打开各种类型文件
intent.setDataAndType(Uri.fromFile(f), type);
// 安装
activity.startActivity(intent);
}
/**
* 删除临时路径里的安装包
*/
public void delFile() {
Log.i(TAG, "The TempFile(" + currentTempFilePath + ") was deleted.");
File myFile = new File(currentTempFilePath);
if (myFile.exists()) {
myFile.delete();
}
}
/**
* 获得下载文件的类型
*
* @param f
* 文件名称
* @return 文件类型
*/
private String getMIMEType(File f) {
String type = "";
// 获得文件名称
String fName = f.getName();
// 获得文件扩展名
String end = fName
.substring(fName.lastIndexOf(".") + 1, fName.length())
.toLowerCase();
if (end.equals("m4a") || end.equals("mp3") || end.equals("mid")
|| end.equals("xmf") || end.equals("ogg") || end.equals("wav")) {
type = "audio";
} else if (end.equals("3gp") || end.equals("mp4")) {
type = "video";
} else if (end.equals("jpg") || end.equals("gif") || end.equals("png")
|| end.equals("jpeg") || end.equals("bmp")) {
type = "image";
} else if (end.equals("apk")) {
type = "application/vnd.android.package-archive";
} else {
type = "*";
}
if (end.equals("apk")) {
} else {
type += "/*";
}
return type;
}
}
最新技术文章: