在 Android 中显示文本

Display text in Android

我知道 System.out.println() 不适用于 Android。
所以我需要另一种方法来打印一些文本。

请帮帮我

我正在使用 Root Tools 库

class superuser  {
     public static Command c ;{
         if (RootTools.isRootAvailable()) {
             System.out.print("Root found!!");
         }
         else{ System.out.print(("NO ROOT!"));


         }
     }
 }

您尝试过使用 logcat 吗? http://developer.android.com/reference/android/util/Log.html

像这样使用它:Log.v("myAwesomeApp", "my developer comment");

然后使用 IDE 中的日志面板阅读它

if(condition){
 Log.d("message","The root found");
 }
else{
Log.d("message","The root not found");
 }

使用logcat是Android中常用的方法,也是最简单的。

Log.d("message_id","message content");

如果你想要其他方式显示日志,你可以试试

https://github.com/orhanobut/logger

正在 android

中输出文本

方法有很多种,但通常我们在测试和调试过程中使用的是日志。该日志对用户不可见,但您可以在 DDMS 中看到它。 据我了解,您想创建一个对话框或向用户显示一个文本视图,以便他们知道 root 是否可用。


1.Logging(用于测试和调试过程)

我们在下面的代码中定义了 TAG,因为以后很容易进行更改并且我们的代码更有条理

private static final String TAG = MyActivity.class.getName();
Log.v(TAG , "here is the line i want to output in logcat");

这里的 v in log.v 代表 verbose.You 可以使用 i 表示信息,e 表示错误等

2.Displaying 通过 TextView 向用户发送文本

首先让我们导入文本视图。让你导入的textview的id可能是“resultTextView”

TextView resultText = (TextView) findViewById(R.id.resultTextView);

现在应用您的逻辑并设置其文本...

     if (RootTools.isRootAvailable()) {
         resultText.setText("Root found!!");
     }
     else{ resultText.setText(("NO ROOT!"));


     }

3.Creating一个对话框

对话框是我们收到的弹出消息。 我建议创建一个将字符串消息和字符串标题作为参数的函数,并使用 dialog.builder 创建一个对话框,而不是对话框片段(在下面的 link 中可用)- http://developer.android.com/reference/android/app/AlertDialog.Builder.html

public void alertDialog(String message,String title){

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
            this);

        // set title
        alertDialogBuilder.setTitle(title);

        // set dialog message
        alertDialogBuilder
            .setMessage(message)
            .setPositiveButton("OK", null) //we write null cause we don't want 
//to perform any action after ok is clicked, we just want the message to disappear 


            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
        }

现在您可以调用带有您想要的标题和文本的方法:)