您的位置:首页 > 移动开发 > Android开发

Android-编程小问题记录

2015-09-01 15:44 489 查看
将自己在开放中遇到的一些小问题不断地记录在这里。

添加library失败

window下,eclipse添加 Android library的时候,项目和library必须是在相同目录下,否则会出错

添加library之后,R文件问题

这里其实是项目的lib包冲突了,只需要将项目里面的libs文件夹下面的对应文件删掉就好了。(遇到的时候解决是删掉的android-support-v4.jar 包)

fragment 与activity通信

如果是在xml里面声明了fragment的话,那么在activity中通过getfragmentmanager()方法获取到fragmentmanger,然后再获取到fragment,就可以自由访问fragment里面的资源了。

如果是在activity的代码中动态添加的,那么应该会有保存的fragment对象的成员,直接访问即可

通过intent传递list对象

参考自一下内容:http://www.cnblogs.com/shaocm/archive/2013/01/08/2851248.html

如果是要传递
list<object>
那么,对应的object类需要实现Serializable。

发送方
Intent.putExtras(key, (Serializable)list)


接收方
(List<YourObject>)getIntent().getSerializable(key)


不显示title bar与全屏

参考自:http://blog.csdn.net/shakespeare001/article/details/7779011

xml设置方法

不显示titlebar
android:theme="@android:style/Theme.NoTitleBar"


全屏
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"


代码设置方法

不显示titlebar
requestWindowFeature(Window.FEATURE_NO_TITLE);


全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);


android 获取手机imei码

手机imei码可以作为区分不同的移动设备的一种方式

关于imei码,参看百度百科:http://baike.baidu.com/link?url=Zd5j5ULJOFFlRLczHBMMwKqcTT7V7pFELi0qncQ5nYwaMLr5E6nGqEWIMmU1H_V6dOUsxCN4wLDimDAb3e76dq

具体方法 参看:http://www.cnblogs.com/luxiaofeng54/archive/2011/03/01/1968063.html

需要在manifest.xml中获取对应的权限

<uses-permission android:name="android.permission.READ_PHONE_STATE" />


获取imei代码

Imei = ((TelephonyManager) getSystemService(TELEPHONY_SERVICE))

.getDeviceId();


fragment 获取view对象

首选获取到fragment整个的一个view

View view;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view =  inflater.inflate(R.layout.fragment_a, container, false);
return view ;


然后,通过上面获取到的view来获取其他对象

比如有一个textview对象

TextView textView1 = (TextView)view.findViewById(R.id.textView1)


获取汉字拼音首字母

方法参看 http://blog.csdn.net/gebitan505/article/details/38398011

核心代码

MainActivity.java

package com.example.firstchar;

import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

static final int GB_SP_DIFF = 160;
// 存放国标一级汉字不同读音的起始区位码
static final int[] secPosValueList = { 1601, 1637, 1833, 2078, 2274, 2302,
2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858, 4027,
4086, 4390, 4558, 4684, 4925, 5249, 5600 };
// 存放国标一级汉字不同读音的起始区位码对应读音
static final char[] firstLetter = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'w', 'x',
'y', 'z' };
private EditText edit;
private TextView text;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText) this.findViewById(R.id.edit);
text = (T
4000
extView) this.findViewById(R.id.textView);
button = (Button) this.findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
text.setText(getFirstChar(edit.getText().toString()).toLowerCase());
}
});

}

public static String getFirstChar(String characters) {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < characters.length(); i++) {

char ch = characters.charAt(i);
if ((ch >> 7) == 0) {
// 判断是否为汉字,如果左移7为为0就不是汉字,否则是汉字
//return ch;
buffer.append(ch);
} else {
char spell = getFirstLetter(ch);
//return spell;
buffer.append(String.valueOf(spell));
}
}
return buffer.toString();
//return '#';
}

// 获取一个汉字的首字母
public static Character getFirstLetter(char ch) {

byte[] uniCode = null;
try {
uniCode = String.valueOf(ch).getBytes("GBK");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
if (uniCode[0] < 128 && uniCode[0] > 0) { // 非汉字
return null;
} else {
return convert(uniCode);
}
}

/**
* 获取一个汉字的拼音首字母。 GB码两个字节分别减去160,转换成10进制码组合就可以得到区位码
* 例如汉字“你”的GB码是0xC4/0xE3,分别减去0xA0(160)就是0x24/0x43
* 0x24转成10进制就是36,0x43是67,那么它的区位码就是3667,在对照表中读音为‘n’
*/
static char convert(byte[] bytes) {
char result = '-';
int secPosValue = 0;
int i;
for (i = 0; i < bytes.length; i++) {
bytes[i] -= GB_SP_DIFF;
}
secPosValue = bytes[0] * 100 + bytes[1];
for (i = 0; i < 23; i++) {
if (secPosValue >= secPosValueList[i]
&& secPosValue < secPosValueList[i + 1]) {
result = firstLetter[i];
break;
}
}
return result;
}

}


acitivity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="输入字符床" >
</EditText>

<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="convert" >
</Button>

<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TextView>

</LinearLayout>


对ArrayList 排序

方法参看 http://blog.csdn.net/faith_boys/article/details/8547605

关键在于实现
Comparator
接口,然后通过调用
Collections.sort(list,comp);
即可

LinerLayout 添加点击事件

和其他的view添加点击事件的方法是一样的,通过setOnclickListener即可。

但是要注意的是如果layout里面的成员也设置了监听事件的话,就有可能接受不到了。

具体参看:http://blog.csdn.net/zanfeng/article/details/41172871

intent在activity之间跳转

Intent intent = new Intent();
//用intent.putExtra(String name, String value);来传递参数。
intent.putExtra("one", num1);
intent.putExtra("two", num2);
intent.setClass(ButtonAndView.this, ResultActivity.class);
startActivity(intent);


LinearLayout实现点击时颜色变动效果

参看 http://blog.csdn.net/yangmingysc/article/details/17413099

主要内容

在Linarlayout中的布局中,
android:background="@drawable/layout_selector"
通过一个xml
layout_selector.xml
来为linarlayout的背景填充

其中
layout_selector.xml
的代码如下

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@color/lightgrey"/>
<item android:state_pressed="true" android:drawable="@color/lightgrey" />
<item android:drawable="@color/white"/>
</selector>


这样即可实现一个linarlayout点击的变化效果

如果颜色没有发生变化的话,有可能是
android:clickable="true"
需要写入linarlayout中。

Android 动态切换主题

如果对于
styles.xml
文件中 的一些内容还不是很清楚的话,参看http://blog.csdn.net/hewence1/article/details/39249463

关于动态切换theme主题的,参看http://blog.csdn.net/wsscy2004/article/details/7562909

下面直接放测试的代码:

styles.xml

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="Theme_Translucent">
<item name="android:background">@android:color/holo_red_dark</item>
</style>
<style name="Theme_Translucent2">
<item name="android:background">@android:color/holo_blue_bright</item>
</style>
<style name="Theme_Transparent">
<item name="android:background">@android:color/darker_gray</item>
</style>
</resources>


activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:textColor="@android:color/holo_green_dark"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/str_text_view1"
/>

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="主题1" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="主题2" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="主题3" />

</LinearLayout>


Util.java

package com.example.admin.dd_test_theme;

import android.app.Activity;
import android.content.Intent;

/**
* Created by admin on 2015/9/5.
*/
public class Util {
private static int sTheme;

public final static int THEME_DEFAULT = 0;
public final static int THEME_WHITE = 1;
public final static int THEME_BLUE = 2;

/**
* Set the theme of the Activity, and restart it by creating a new Activity
* of the same type.
*/
public static void changeToTheme(Activity activity, int theme)
{
sTheme = theme;
activity.finish();

activity.startActivity(new Intent(activity, activity.getClass()));
}

/** Set the theme of the activity, according to the configuration. */
public static void onActivityCreateSetTheme(Activity activity)
{
switch (sTheme)
{
default:
case 1:
activity.setTheme(R.style.Theme_Translucent);
break;
case 2:
activity.setTheme(R.style.Theme_Translucent2);
break;
case 3:
activity.setTheme(R.style.Theme_Transparent);
break;
}
}
}


MainActivity.java

package com.example.admin.dd_test_theme;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener {
private Button button = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
Util.onActivityCreateSetTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);

}

@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button1:
System.out.println("主题1");
Util.changeToTheme(this, 1);
break;
case R.id.button2:
System.out.println("主题2");
Util.changeToTheme(this, 2);
break;
case R.id.button3:
System.out.println("主题3");
Util.changeToTheme(this, 3);
break;
}
}
}


Android 粘贴版操作

ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText("some thing");
clipboard.getText();


不过现在貌似已经不推荐用set和get直接设置值了,具体的可以再查看一些资料

R文件消失的问题

今天又遇到一次R文件没有了的问题,纠结了很久。后来发现在console里面其实是有提示的,显示的是某个drawable文件的问题,于是去fix了,然后就好了。==|||

漂亮的dialog

使用github上面的开源项目 https://github.com/pedant/sweet-alert-dialog

在release里面可以找到一个sample的源码

eclipse 折叠所有代码快捷键

进入 windows->perferences->general->key

代码折叠的快捷键,默认是:

Ctrl+Shift+Numpad_Divede(小键盘的/号)

Ctrl+Shift+Numpad_Multiply(小键盘的*号)

不过由于笔记本没有Numpad,所以可以修改快捷键之后再使用

ActionBar 样式修改

Home部分,参看:http://blog.csdn.net/zzzzyu/article/details/40072819

隐藏图标:http://blog.csdn.net/jdsjlzx/article/details/41353029

隐藏或显示actionBar:
getActionBar.hide();


标题与菜单中的文字样式:http://www.cnblogs.com/angeldevil/p/3836214.html

自定义菜单文字样式代码:

<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionBarStyle">@style/CustomActionBar</item>
<item name="android:actionMenuTextAppearance">@style/ActionBarTitle</item>
<item name="android:actionMenuTextColor">@android:color/holo_blue_light</item>
</style>

<style name="ActionBarMenu" parent="@android:style/TextAppearance.Holo.Large">
<item name="android:textColor">@android:color/holo_blue_light</item>
<item name="android:textSize">20sp</item>
</style>


Android 界面大小问题

参看这些数据 http://www.alibuybuy.com/posts/85486.html

Android 生命周期问题

参看 http://blog.csdn.net/sirnuo/article/details/21164693
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息