如何允许硬编码字符串并停止询问使用strings.xml

时间:2016-01-02 01:53:40

标签: android string android-studio

我目前正在开发一个Android项目,我没有任何计划将它翻译成其他语言,所以我不会在strings.xml中保存字符串文字。但是,每当我硬编码字符串文字时,Android Studio都会抱怨,特别是在设置TextView的文本值时。

enter image description here

enter image description here

enter image description here

有没有办法禁用这些警告?

4 个答案:

答案 0 :(得分:8)

您可以在以下位置进行编辑设置 - >编辑 - >检查 - > Android Lint-> TextView国际化:

Editor

和xml设置 - >编辑器 - >检查 - > Android Lint->硬编码文字:

XML

答案 1 :(得分:3)

添加

@SuppressLint("SetTextI18n")

在你的功能之上。 例如:

@SuppressLint("SetTextI18n")
private void updateEZWBMode2ConnectionStatus(){
    switch (IsConnected.checkMode2(mContext)){
        case IsConnected.USB_TETHERING:
            M2StatusTV.setText("Status: Connected");
            M2StatusTV.setTextColor(Color.argb(255,0,255,0));
            M2ReceiveTV.setVisibility(View.VISIBLE);
            startTestReceiverSafe(M2ReceiveTV);
            break;
        case IsConnected.USB_CONNECTED:
            M2StatusTV.setText("Status: No Tethering");
            M2StatusTV.setTextColor(Color.argb(255,255,51,51));
            M1ReceiveTV.setVisibility(View.GONE);
            M2ReceiveTV.setVisibility(View.GONE);
            stopTestReceiverSafe();
            break;
        case IsConnected.USB_NOTHING:
            M2StatusTV.setText("Status: No USB Connection");
            M2StatusTV.setTextColor(Color.argb(255,255,51,51));
            M1ReceiveTV.setVisibility(View.GONE);
            M2ReceiveTV.setVisibility(View.GONE);
            stopTestReceiverSafe();
            break;
    }
}

答案 2 :(得分:1)

老问题,但无论如何,被接受的答案是令人误解的。

不需要翻译字符串文字资源。实际上,它们可以是资源文件中的marked as non-translatable。这样,您仍然会坚持使用best practices,而不会因皮棉和翻译而烦恼。

<string name="invite_sent" translatable="false">Invite sent</string>

虽然禁用Lint可以使您不再烦恼,但还有另一个原因(您希望(并且应该))使用字符串文字资源:重复。遵循DRY(Don't Repeat Yourself)原则可以避免一系列繁琐的问题,从复杂的重构到意外的行为,以及在使用该应用程序时用户体验的不一致。试想一下,“ OK”按钮出现在10多个屏幕中。单一的参考和来源简化并集中了项目的维护。

答案 3 :(得分:1)

我认为最好的方法是使用gradle文件,这将使您无需在Android Studio中就可以全局抑制这些文件,因此您的设置也可以进入源代码控制,然后不必单独装饰要应用警告的每种方法。为此,请在gradle文件的lint选项中禁用SetTextI18n,如下所示:

private void uploadImageToFirebase(Bitmap bmp) {

    try {
        String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
                "/news";
        File dir = new File(file_path);
        if (!dir.exists())
            dir.mkdirs();
        File file = new File(dir, "sketchpad" + "_" + ".png");
        FileOutputStream fOut = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
        Log.i("SECONDACTIVITY", "ADDED SUCCESSFULLY");

        Uri file2 = Uri.fromFile(file);


        //Now Upload
        final StorageReference storageRef = FirebaseStorage.getInstance().getReference();
        StorageReference riversRef = storageRef.child(file.getName());
        UploadTask uploadTask;
        uploadTask = riversRef.putFile(file2);
        progressDialog.show();
        progressDialog.setCancelable(false);


        uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
                progressDialog.incrementProgressBy((int) progress);
            }
        });
        uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                progressDialog.dismiss();
                Uri DownloadLink = taskSnapshot.getDownloadUrl();
                String imgUrl = DownloadLink.toString();
                FirebaseDatabase.getInstance().getReference().child("image_link").setValue(imgUrl);
            }
        });
        uploadTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                progressDialog.dismiss();
            }
        });


    } catch (IOException e) {
        e.printStackTrace();
    }


}

分级同步和瞧,警告消失了。

相关问题