Android APP顯示訊息方法(dirty ways)
使用Toast顯示訊息
import android.widget.Toast;Toast.makeText(getApplicationContext(), "your message", Toast.LENGTH_SHORT).show();
如果怕太多Toast可以用cancel
import android.widget.Toast;private Toast mToast = null;
protected void showMessage(String message) {
final String msg = message;
runOnUiThread(new Runnable(){
public void run() {
try {
Log.i(TAG, msg);
if (mToast!=null) {
mToast.cancel();
mToast = null;
}
mToast = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG);
if (mToast!=null) mToast.show();
} catch (Exception e) {
e.printStackTrace();
}
}// end run
});
}
使用TextView顯示訊息
In activity_main.xmlandroid:maxLines="15"
android:singleLine="false"
android:textAlignment="viewStart"
/>
import android.widget.TextView;
In MainActivity class
private TextView uiResult;
protected void showResult(final String msg) {
runOnUiThread(new Runnable() {
@Override
public void run() {
uiResult.append(msg);
}
});
}
In onCreate
uiResult = (TextView)findViewById(R.id.textResult);
In your code
showResult("your message\n");
輸出到Logcat
import android.util.Log;public static String TAG = "you defined name";
Log.i(TAG, "your message");
Log.w(TAG, "your message");
Log.e(TAG, "your message");
Log.d(TAG, "your message");
Log.v(TAG, "your message");
留言