当前位置: 编程技术>移动开发
本页文章导读:
▪Looper物品之角色(Demo 3) Looper物件之角色(Demo 3)
这个例子是为了更进一步的理解Demo2 这个例子跟Demo2只是有些小的改动,大家可以对比的看下 Java代码 package com.example.Looper_03; import android.app.Activity; import an.........
▪ 直接途过程吧 直接说过程吧:
直接说过程吧: 1.构建一个HashMap: Java代码 ArrayList<HashMap<String, String>> dlist = new ArrayList<HashMap<String, String>>(); for (int i = 0; i < data.length; i++) { .........
▪ 运用获取手机本地图片资源 应用获取手机本地图片资源
private Button mGetImg; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInsta.........
[1]Looper物品之角色(Demo 3)
来源: 互联网 发布时间: 2014-02-18
Looper物件之角色(Demo 3)
这个例子是为了更进一步的理解Demo2
这个例子跟Demo2只是有些小的改动,大家可以对比的看下
Java代码
package com.example.Looper_03;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Looper_03 extends Activity implements OnClickListener {
private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
private final int FP = LinearLayout.LayoutParams.FILL_PARENT;
public TextView tv;
private myThread t;
private Button btn, btn2;
EventHandler h;
Context ctx;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
ctx = this;
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
btn = new Button(this);
btn.setId(101);
btn.setBackgroundResource(R.drawable.icon);
btn.setText("test looper");
btn.setOnClickListener(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(100, 50);
param.topMargin = 10;
layout.addView(btn, param);
btn2 = new Button(this);
btn2.setId(102);
btn2.setBackgroundResource(R.drawable.icon);
btn2.setText("exit");
btn2.setOnClickListener(this);
layout.addView(btn2, param);
tv = new TextView(this);
tv.setTextColor(Color.WHITE);
tv.setText("");
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(FP, WC);
param2.topMargin = 10;
layout.addView(tv, param2);
setContentView(layout);
}
public void onClick(View v) {
switch (v.getId()) {
case 101:
// 此h是屬於main線程的(用來存取主線程的MessageQueue)
//h = new EventHandler(Looper.myLooper());//@
//或者可以写成这样;
h = new EventHandler();
t = new myThread();
t.start();
break;
case 102:
finish();
break;
}
}
//或者可以写成这样;
public class EventHandler extends Handler {
@Override
public void handleMessage(Message msg) {
((Activity) ctx).setTitle((String) msg.obj);
}
}
// ------------------------------------------------
/*public class EventHandler extends Handler {//@
public EventHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
((Activity) ctx).setTitle((String) msg.obj);
}
}*/
// ------------------------------------------------------
class myThread extends Thread {
public void run() {
String obj = "from myThread";
Message m = h.obtainMessage(1, 1, 1, obj);
//这里还是通过主线程的handler来传送消息
//所以还是由主线程的Looper来接受消息
h.sendMessage(m);
}
}
}
// ------------------------------------------------------
这个例子是为了更进一步的理解Demo2
这个例子跟Demo2只是有些小的改动,大家可以对比的看下
Java代码
package com.example.Looper_03;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Looper_03 extends Activity implements OnClickListener {
private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
private final int FP = LinearLayout.LayoutParams.FILL_PARENT;
public TextView tv;
private myThread t;
private Button btn, btn2;
EventHandler h;
Context ctx;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
ctx = this;
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
btn = new Button(this);
btn.setId(101);
btn.setBackgroundResource(R.drawable.icon);
btn.setText("test looper");
btn.setOnClickListener(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(100, 50);
param.topMargin = 10;
layout.addView(btn, param);
btn2 = new Button(this);
btn2.setId(102);
btn2.setBackgroundResource(R.drawable.icon);
btn2.setText("exit");
btn2.setOnClickListener(this);
layout.addView(btn2, param);
tv = new TextView(this);
tv.setTextColor(Color.WHITE);
tv.setText("");
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(FP, WC);
param2.topMargin = 10;
layout.addView(tv, param2);
setContentView(layout);
}
public void onClick(View v) {
switch (v.getId()) {
case 101:
// 此h是屬於main線程的(用來存取主線程的MessageQueue)
//h = new EventHandler(Looper.myLooper());//@
//或者可以写成这样;
h = new EventHandler();
t = new myThread();
t.start();
break;
case 102:
finish();
break;
}
}
//或者可以写成这样;
public class EventHandler extends Handler {
@Override
public void handleMessage(Message msg) {
((Activity) ctx).setTitle((String) msg.obj);
}
}
// ------------------------------------------------
/*public class EventHandler extends Handler {//@
public EventHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
((Activity) ctx).setTitle((String) msg.obj);
}
}*/
// ------------------------------------------------------
class myThread extends Thread {
public void run() {
String obj = "from myThread";
Message m = h.obtainMessage(1, 1, 1, obj);
//这里还是通过主线程的handler来传送消息
//所以还是由主线程的Looper来接受消息
h.sendMessage(m);
}
}
}
// ------------------------------------------------------
[2] 直接途过程吧
来源: 互联网 发布时间: 2014-02-18
直接说过程吧:
直接说过程吧:
1.构建一个HashMap:
Java代码
ArrayList<HashMap<String, String>> dlist = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < data.length; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("data", data[i]);
dlist.add(map);
}
ArrayList<HashMap<String, String>> dlist = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < data.length; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("data", data[i]);
dlist.add(map);
}
2.设置SimpleAdapter:
Java代码
SimpleAdapter adapter = new SimpleAdapter(this, dlist,
R.layout.list_item, new String[] { "data" },
new int[] { R.id.entries });
list.setAdapter(adapter);
SimpleAdapter adapter = new SimpleAdapter(this, dlist,
R.layout.list_item, new String[] { "data" },
new int[] { R.id.entries });
list.setAdapter(adapter);
3.主要在list_item上:
Java代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@drawable/selector">
<TextView android:text="data" android:id="@+id/entries"
android:layout_width="match_parent" android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@drawable/selector">
<TextView android:text="data" android:id="@+id/entries"
android:layout_width="match_parent" android:layout_height="wrap_content">
</TextView>
</LinearLayout>
4.上述item上设置了背景:selector.xml:
Java代码
<item android:state_selected="true">
<shape>
<gradient android:angle="270" android:endColor="#ffffffff"
android:startColor="#ffffffff" />
<padding android:left="15dp" android:top="20dp"
android:right="15dp" android:bottom="20dp" />
<size android:height="60dp" android:width="320dp" />
<corners android:radius="8dp" />
</shape>
</item>
<item android:state_selected="true">
<shape>
<gradient android:angle="270" android:endColor="#ffffffff"
android:startColor="#ffffffff" />
<padding android:left="15dp" android:top="20dp"
android:right="15dp" android:bottom="20dp" />
<size android:height="60dp" android:width="320dp" />
<corners android:radius="8dp" />
</shape>
</item>
"android:state_selected="true""这个属性还有:"android:state_pressed="true""或者不设置表示默认的时候背景情况.
我写在这里是为了后续编码的时候参考的......:)
直接说过程吧:
1.构建一个HashMap:
Java代码
ArrayList<HashMap<String, String>> dlist = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < data.length; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("data", data[i]);
dlist.add(map);
}
ArrayList<HashMap<String, String>> dlist = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < data.length; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("data", data[i]);
dlist.add(map);
}
2.设置SimpleAdapter:
Java代码
SimpleAdapter adapter = new SimpleAdapter(this, dlist,
R.layout.list_item, new String[] { "data" },
new int[] { R.id.entries });
list.setAdapter(adapter);
SimpleAdapter adapter = new SimpleAdapter(this, dlist,
R.layout.list_item, new String[] { "data" },
new int[] { R.id.entries });
list.setAdapter(adapter);
3.主要在list_item上:
Java代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@drawable/selector">
<TextView android:text="data" android:id="@+id/entries"
android:layout_width="match_parent" android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@drawable/selector">
<TextView android:text="data" android:id="@+id/entries"
android:layout_width="match_parent" android:layout_height="wrap_content">
</TextView>
</LinearLayout>
4.上述item上设置了背景:selector.xml:
Java代码
<item android:state_selected="true">
<shape>
<gradient android:angle="270" android:endColor="#ffffffff"
android:startColor="#ffffffff" />
<padding android:left="15dp" android:top="20dp"
android:right="15dp" android:bottom="20dp" />
<size android:height="60dp" android:width="320dp" />
<corners android:radius="8dp" />
</shape>
</item>
<item android:state_selected="true">
<shape>
<gradient android:angle="270" android:endColor="#ffffffff"
android:startColor="#ffffffff" />
<padding android:left="15dp" android:top="20dp"
android:right="15dp" android:bottom="20dp" />
<size android:height="60dp" android:width="320dp" />
<corners android:radius="8dp" />
</shape>
</item>
"android:state_selected="true""这个属性还有:"android:state_pressed="true""或者不设置表示默认的时候背景情况.
我写在这里是为了后续编码的时候参考的......:)
[3] 运用获取手机本地图片资源
来源: 互联网 发布时间: 2014-02-18
应用获取手机本地图片资源
private Button mGetImg;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
}
public void findView() {
mGetImg = (Button) findViewById(R.id.mGetLocalImg);
mGetImg.setOnClickListener(this);
}
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
// intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 1:
Uri uri = data.getData();
ContentResolver cr = getContentResolver();
Cursor mCur = cr.query(uri, null, null, null, null);
/** 得到本地图片库中图片的 id、路径、大小、文件名 */
// cursor.getString(0),1,2,3
if(!mCur.moveToFirst()){
return;
}
try {
String mImgPath = mCur.getString(mCur.getColumnIndex("_data"));
System.out.println("mImgPath__________" + mImgPath);
// InputStream in = cr.openInputStream(uri);
// Bitmap mBitmap = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}
private Button mGetImg;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
}
public void findView() {
mGetImg = (Button) findViewById(R.id.mGetLocalImg);
mGetImg.setOnClickListener(this);
}
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
// intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 1:
Uri uri = data.getData();
ContentResolver cr = getContentResolver();
Cursor mCur = cr.query(uri, null, null, null, null);
/** 得到本地图片库中图片的 id、路径、大小、文件名 */
// cursor.getString(0),1,2,3
if(!mCur.moveToFirst()){
return;
}
try {
String mImgPath = mCur.getString(mCur.getColumnIndex("_data"));
System.out.println("mImgPath__________" + mImgPath);
// InputStream in = cr.openInputStream(uri);
// Bitmap mBitmap = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}
最新技术文章: