** 可以改用 java.nio.charset.StandardCharsets.UTF_8 ,但至少要 API 19
java
1
2
3
InputStream is = cxt.getResources().openRawResource(R.raw.car_type_list);
String content = IOUtils.toString(is, Charsets.UTF_8);
IOUtils.closeQuietly(is);// don't forget to close your streams
publicclassPhotoListDialogFragmentextends DialogFragment {private String img;publicPhotoListDialogFragment(String img){this.img= img;}@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState){// Use the Builder class for convenient dialog construction
View content = LayoutInflater.from(getContext()).inflate(R.layout.photo_list_dialog_content,null,false);
TextView iv =(TextView) content.findViewById(R.id.photoListDialogIV);
iv.setText(img);
AlertDialog.Builder builder =new AlertDialog.Builder(getActivity(), R.style.DialogStyle);
builder.setView(content).setPositiveButton(R.string.dialog_ok,new DialogInterface.OnClickListener(){publicvoidonClick(DialogInterface dialog,int id){// FIRE ZE MISSILES!}});// Create the AlertDialog object and return itreturn builder.create();}}
A value type is a type whose value is copied when it is assigned to a variable or constant, or when it is passed to a function.
You’ve actually been using value types extensively throughout the previous chapters. In fact, all of the basic types in Swift—integers, floating-point numbers, Booleans, strings, arrays and dictionaries—are value types, and are implemented as structures behind the scenes.
All structures and enumerations are value types in Swift. This means that any structure and enumeration instances you create—and any value types they have as properties—are always copied when they are passed around in your code.
Assignment and Copy Behavior for Strings, Arrays, and Dictionaries
In Swift, many basic data types such as String, Array, and Dictionary are implemented as structures. This means that data such as strings, arrays, and dictionaries are copied when they are assigned to a new constant or variable, or when they are passed to a function or method.
This behavior is different from Foundation: NSString, NSArray, and NSDictionary are implemented as classes, not structures. Strings, arrays, and dictionaries in Foundation are always assigned and passed around as a reference to an existing instance, rather than as a copy.
Constant and Variable Parameters
Function parameters are constants by default. Trying to change the value of a function parameter from within the body of that function results in a compile-time error. This means that you can’t change the value of a parameter by mistake.
However, sometimes it is useful for a function to have a variable copy of a parameter’s value to work with. You can avoid defining a new variable yourself within the function by specifying one or more parameters as variable parameters instead. Variable parameters are available as variables rather than as constants, and give a new modifiable copy of the parameter’s value for your function to work with.
Define variable parameters by prefixing the parameter name with the var keyword:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func alignRight(var string: String, totalLength: Int, pad: Character) -> String {
let amountToPad = totalLength - string.characters.count
if amountToPad < 1 {
return string
}
let padString = String(pad)
for _ in 1...amountToPad {
string = padString + string
}
return string
}
let originalString = "hello"
let paddedString = alignRight(originalString, totalLength: 10, pad: "-")
// paddedString is equal to "-----hello"
// originalString is still equal to "hello"
In-Out Parameters
Variable parameters, as described above, can only be changed within the function itself. If you want a function to modify a parameter’s value, and you want those changes to persist after the function call has ended, define that parameter as an in-out parameter instead.
1
2
3
4
5
6
7
8
9
10
11
func swapTwoInts(inout a: Int, inout _ b: Int) {
let temporaryA = a
a = b
b = temporaryA
}
var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt)
print("someInt is now \(someInt), and anotherInt is now \(anotherInt)")
// prints "someInt is now 107, and anotherInt is now 3"
On Android devices, GCM uses an existing connection for Google services. For pre-3.0 devices, this requires users to set up their Google accounts on their mobile devices. A Google account is not a requirement on devices running Android 4.0.4 or higher.
// Check if the Camera permission is already available.if( ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)== PackageManager.PERMISSION_GRANTED){// already available}else{// asking whether to allow permission...
requestCameraPermission();}
/** * Requests the Camera permission. * If the permission has been denied previously, a SnackBar will prompt the user to grant the * permission, otherwise it is requested directly. */privatevoidrequestCameraPermission(){
Log.i( TAG,"CAMERA permission has NOT been granted. Requesting permission.");/** * shouldShowRequestPermissionRationale 回傳的規則如下: * 1.true:用戶在先前權限詢問的選項中,選擇了「拒絕」,但沒有勾選「不再詢問」 * 2.false:第一次安裝APP後進入該功能 * 3.false:用戶在先前權限詢問選項中,選擇了「允許」 * 4.false:用戶在先前權限詢問的選項中,選擇了「拒絕」,但勾選「不再詢問」 * 5.false:用戶在「設定」裡頭選擇「允許或拒絕」 * * 呼叫requestPermissions後,是否會跳出詢問視窗,規則如下: * 1.不會:用戶在先前權限詢問選項中,選擇了「允許」 * 2.不會:用戶在先前權限詢問的選項中,選擇了「拒絕」,但勾選「不再詢問」,在後續onRequestPermissionsResult當中一律回傳PERMISSION_DENIED * 3.會:第一次安裝APP後進入該功能(第一次跳出不會有「不再詢問」的選項可以勾選) * 4.會:用戶在先前權限詢問的選項中,選擇了「拒絕」 * 5.會:用戶在「設定」裡頭選擇「允許或拒絕」 * * 當呼叫requestPermissions後,不管用戶選擇允許或拒絕,又或者沒有跳出詢問視窗,後續都會進入onRequestPermissionsResult執行後續動作,規則如下: * 1.PERMISSION_GRANTED(允許):用戶選擇「允許」選項、設定中直接選擇「允許」 * 2.PERMISSION_DENIED(拒絕):用戶選擇「拒絕」選項、設定中直接選擇「拒絕」 */if( ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)){// Provide an additional rationale to the user if the permission was not granted// and the user would benefit from additional context for the use of the permission.// For example if the user has previously denied the permission.
Log.i( TAG,"Displaying camera permission rationale to provide additional context.");// 「R.string.permission_camera_rationale」的內容可以多加一些說明提醒使用戶該功能必須允許該權限才可以使用
Snackbar.make( mLayout, R.string.permission_camera_rationale, Snackbar.LENGTH_INDEFINITE).setAction( R.string.ok,new View.OnClickListener(){@OverridepublicvoidonClick( View view ){
ActivityCompat.requestPermissions( MainActivity.this,new String[]{ Manifest.permission.CAMERA}, REQUEST_CAMERA );}}).show();}else{// Camera permission has not been granted yet. Request it directly.
ActivityCompat.requestPermissions(this,new String[]{ Manifest.permission.CAMERA}, REQUEST_CAMERA );}}
/** * Callback received when a permissions request has been completed. */@OverridepublicvoidonRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNullint[] grantResults ){if( requestCode == REQUEST_CAMERA ){// Received permission result for camera permission.
Log.i( TAG,"Received response for Camera permission request.");// Check if the only required permission has been grantedif( grantResults.length==1&& grantResults[0]== PackageManager.PERMISSION_GRANTED){// Camera permission has been granted, preview can be displayed
Log.i( TAG,"CAMERA permission has now been granted. Showing preview.");
Snackbar.make( mLayout, R.string.permision_available_camera, Snackbar.LENGTH_SHORT).show();}else{
Log.i( TAG,"CAMERA permission was NOT granted.");
Snackbar.make( mLayout, R.string.permissions_not_granted, Snackbar.LENGTH_SHORT).show();}}// other callbackelse{super.onRequestPermissionsResult( requestCode, permissions, grantResults );}}