用按钮代替Spinner,弹出的对话框,截图和代码请见附件!
用spinner实现对话框
public class SpinnerUseInArea extends Activity { Spinner mySpinner; private static final String[] countriesStr = { "111", "222", "333", "444" }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mySpinner = (Spinner) findViewById(R.id.mySpinner); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countriesStr); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); mySpinner.setAdapter(adapter); } }
用按钮实现
public class EX03_12 extends Activity { private Button mButton1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mButton1 = (Button) findViewById(R.id.myButton1); mButton1.setOnClickListener(new OnClickListener() { public void onClick(View v) { new AlertDialog.Builder(EX03_12.this) // .setTitle("niaho") .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { /* User clicked so do some stuff */ /* String[] items = getResources().getStringArray(R.array.select_dialog_items); new AlertDialog.Builder(EX03_12.this) .setMessage("You selected: " + which + " , " + items[which]) .show();*/ } }).show(); } }); } }
import java.lang.Math;
import android.view.View;
import android.view.ViewGroup;
import android.content.Context;
import android.util.AttributeSet;
public class SideLayout extends ViewGroup {
private int max_wid, max_hgt;
public SideLayout(Context ctx) {
super(ctx);
}
public SideLayout(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
}
public SideLayout(Context ctx, AttributeSet attrs, int defStyle) {
super(ctx, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
final int num_children = getChildCount();
// Since this is a special-purpose widget, this is brute simple. All
// of our children will have sizes of wrap_content, so we query them
// all, then make a second pass assigning their final sizes.
max_wid = 0;
max_hgt = 0;
for( int i = 0; i < num_children; ++i )
{
final View child = getChildAt(i);
if( child != null && child.getVisibility() != View.GONE )
{
measureChild(child, widthMeasureSpec, heightMeasureSpec);
max_wid = Math.max(max_wid, child.getMeasuredWidth());
max_hgt = Math.max(max_hgt, child.getMeasuredHeight());
}
}
// Make a second pass, tell children the actual size they got.
for( int i = 0; i < num_children; ++i )
{
final View child = getChildAt(i);
if( child != null && child.getVisibility() != View.GONE )
{
child.measure(
MeasureSpec.makeMeasureSpec(max_wid, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(max_hgt, MeasureSpec.EXACTLY));
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
final int num_children = getChildCount();
int x,y;
if( num_children <= 0 )
return;
// Split the children into two groups, left and right. Each
// column is evenly-spaced.
int wid = r - l;
int hgt = b - t;
int nrow = (num_children + 1) / 2;
int margin = (hgt - max_hgt * nrow) / (nrow + 1);
int i = 0;
for( int col = 0; col < 2; ++col ) {
x = col == 0 ? 0 : wid - max_wid;
y = margin;
for( int row = 0; row < nrow; ++row, ++i ) {
if( i < num_children ) {
final View child = getChildAt(i);
if( child != null && child.getVisibility() != View.GONE )
child.layout(x, y, x+max_wid, y+max_hgt);
}
y += margin + max_hgt;
}
}
}
}
有些手机可以获取本身的sim号码,有些手机是获取不到的,htc1.5能获取 在1.6版本上就获取不到了,不过打至方法一样,
public class TestShowNum extends Activity { /** Called when the activity is first created. */ TextView textView; Button button; String tel; String deviceid ; String imei; String imsi; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button=(Button) findViewById(R.id.butto); textView=(TextView) findViewById(R.id.text); TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); deviceid = tm.getDeviceId(); tel = tm.getLine1Number(); imei = tm.getSimSerialNumber(); imsi = tm.getSubscriberId(); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub textView.setText(deviceid+"\n"+tel+"\n"+"\n"+imei+"\n"+imsi); } }); } }
在AndroidManifest.xml中加上
<uses-permission android:name="android.permission.READ_PHONE_STATE" />