long in java example
Android development
long is 64 bit signed type and used when int is not large enough to hold the value.
long je celé číslo 64 bitů -9223372036854775808 +9223372036854775807 a používá se tam, kde typ int není schopen pojmout takovou hodnotu čísla.
long is 64 bit signed type and used when int is not large enough to hold the value.
long je celé číslo 64 bitů -9223372036854775808 +9223372036854775807 a používá se tam, kde typ int není schopen pojmout takovou hodnotu čísla.
// declaration and assignment of value type long
long n = 22337203685477580L;
// print formated value
System.out.printf("The value of x is %d%n", n); // 22337203685477580
System.out.format("%+,8d%n%n", n); // +22 337 203 685 477 580
// declaring more variables in single statement
long lo1 = 12L, lo2 = 56, lo3 = 1455555555589L;
// long range of value
System.out.println(Long.MAX_VALUE); // 9223372036854775807
System.out.println(Long.MIN_VALUE); // -9223372036854775808
// check if a string is a valid number in Java example
// convert string to long Java example
String sLong = "1288888888888888";
long longParse = Long.parseLong(sLong);
// convert strings to numbers
long longFromString = (Long.valueOf(sLong)).longValue();
// long to string in Java example code
Long longObj = new Long(229999999999L);
String str = longObj.toString();
// else
Long longS = 888888888888L;
String strLong = longS.toString();
// compare two long variables
Long longComp1 = 5555L;
if (longComp1.equals(55555555L))
System.out.print("true");
// compares the two specified long values in Java example
int i = longS.compareTo(444444L); // -1 first < second
// 0 first == second
// 1 first > second
System.out.print(i);
396LW NO topic_id
AD
Další témata ....(Topics)
How to change the image dynamically in ImageView and button setOnClickListener Android sample.
Main.java
main.xml
Put into res/drawable this pictures:
Main.java
ipackage cz.okhelp.my_game;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class Main extends Activity {
private ImageView hImageViewSemafor;
private Button hButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hImageViewSemafor = (ImageView)findViewById(R.id.idImageViewSemafor);
hButton = (Button) findViewById(R.id.idBtnChangeImage);
hButton.setOnClickListener(mButtonChangeImageListener);
}
View.OnClickListener mButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
// setImageResource will change image in ImageView
hImageViewSemafor.setImageResource(R.drawable.semafor_green);
}
};
}
main.xml
Put into res/drawable this pictures:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView
android:src="@drawable/semafor_red"
android:id="@+id/idImageViewSemafor"
android:background="#66FFFFFF"
android:adjustViewBounds="true"
android:maxWidth="47dip"
android:maxHeight="91dip"
android:padding="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
- File->Project Structure Ctrl + Alt + Shift + S
- Select module and Dependencies Tab
- Click on + PLUS button (right upper corner)
- Select library
Click OK, OK
Get key by value from Map Java Android example
MainClass.java
MainClass.java
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class MainClass {
public static void main(String[] arg) {
// english;germany dictionary
String[] arrayOfString = { "one;eine", "two;zwei", "two sets of;zwei"
, "three;drei", "four;vier" };
Map<String, String> map = new TreeMap<String, String>();
for (String s : arrayOfString) {
String[] array = s.split(";");
String sKey = "", sValue = "";
if (array.length > 1) {
sKey = array[0];
sValue = array[1];
map.put(sKey, sValue);
}
}
if (map.containsValue("zwei")) {
Set<String> references = getKeysByValue(map, "zwei");
Iterator<String> it = references.iterator();
while (it.hasNext()) {
String key = (String) it.next();
String value = map.get(key);
System.out.println(key + " = " + value);
}
}
}
public static <T, E> Set<T> getKeysByValue(Map<T, E> map, E value) {
Set<T> keys = new HashSet<T>();
for (Entry<T, E> entry : map.entrySet()) {
if (entry.getValue().equals(value)) {
keys.add(entry.getKey());
}
}
return keys;
}
}
/*
two = zwei
two sets of = zwei
*/
package cz.okhelp.surfview;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class MainActivity extends Activity implements OnTouchListener {
OurView v;
Bitmap ball, blob;
float x, y;
Sprite sprite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_our_view);
v = new OurView(this);
v.setOnTouchListener(this);
ball = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
x = y = 0;
setContentView(v);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
v.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
v.resume();
}
public class OurView extends SurfaceView implements Runnable {
Thread t = null;
SurfaceHolder holder;
boolean isItOK = false;
public OurView(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}
public void run() {
// TODO Auto-generated method stub
while (isItOK == true) {
// perform canvas drawing
if (!holder.getSurface().isValid()) {
continue;
}
// sprite = new Sprite(OurView.this,blob);
Canvas c = holder.lockCanvas();
c.drawARGB(255, 100, 100, 10);
c.drawBitmap(ball, x, y, null);
// onDraw(c);
holder.unlockCanvasAndPost(c);
}
}
protected void onDraw(Canvas canvas) {
// sprite.onDraw(canvas);
}
public void pause() {
isItOK = false;
while (true) {
try {
t.join();
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
break;
}// end while
}
public void resume() {
isItOK = true;
t = new Thread(this);
t.start();
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
break;
case MotionEvent.ACTION_UP:
x = event.getX();
y = event.getY();
break;
case MotionEvent.ACTION_MOVE:
x = event.getX();
y = event.getY();
break;
}// end switch
return true;
}
}
/data/data/<package>/shared_prefs/<package>_preferences.xml
String TAG = "MyActivityName";
File f = new File("/data/data/"+this.getPackageName()
+"/shared_prefs/"+this.getPackageName()+"_preferences.xml");
if(f.exists())
Log.d(TAG, "exist");
else
Log.d(TAG, "not exist");
Editace: 2011-09-26 20:49:35
Počet článků v kategorii: 396
Url:long-in-java-example