首页 > 解决方案 > 如何在 Android TextView 中显示 PHP 内容?

问题描述

我是新来的,但作为一名程序员,我在 android 中是绿色的。不幸的是,我的搜索没有成功:(

我想在 TextView 中显示 index.php 文件的所有内容。那可能吗?

到目前为止我的代码:

资源/布局/activity_php.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@color/colorPrimaryDark" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_back"
            android:layout_width="40dp"
            android:layout_height="match_parent"
            android:background="@drawable/ic_back" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textAlignment="center"
            android:text="@string/title_php"
            android:gravity="center_vertical"
            android:textSize="20dp"
            android:textColor="@color/colorAccent" />

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/view_php"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="30dp" >

        <TextView
            android:id="@+id/view_php_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Here I wanna see the index.php content" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/btn_php"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_centerInParent="true"
            android:text="Show content"
            android:textColor="@color/colorPrimaryDark"
            android:background="@color/colorAccent"
            android:textSize="8dp" />

    </RelativeLayout>

</RelativeLayout>

java/projectName/functions/PhpActivity

package com.packageName.projectName;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class PhpActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_php);

        Button btnBack = findViewById(R.id.btn_back);
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent closeView = new Intent(PhpActivity.this, MainActivity.class);
                startActivity(closeView);
                finish();
            }
        });

        Button btnPHP = findViewById(R.id.btn_php);
        btnPHP.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Here I wanna put a trigger to start the process
                Toast.makeText(TestaActivity.this, "Recieving", Toast.LENGTH_LONG).show();
            }
        });

    }
}

php 文件的路径是这样的: http ://www.myDomainName.com/chat/log/index.php

并添加

 <uses-permission android:name="android.permission.INTERNET" />

app/manifests/AndroidManifest.xml文件上

PHP 有一些 HTML 内容和 MySQL 查询的结果

<?php
  require_once("head.php");
  require_once("loging-logic.php");

  if(userLoged()) {
    echo "<p class='text-success'>You're logged <?= userLogged() ?>. <a href='logout.php'>Logout?</a></p>";
  } else {
    header("Location: botID.php");
    die();
  }
?>

  <h1>Log Page</h1>

<?php
  require_once("conect.php");
  require_once("body.php");
?>

  <table class="table table-striped table-bordered">

    <?php
      $chatLogs = logList($conDB);
      foreach($chatLogs as $log) :
    ?>

    <tr>
        <td><?= $device['devName'] ?></td>
        <td><?= $type['id'] ?></td>
        <td><?= substr($log['description'], 0, 60) ?></td>
        <td>
          <form action="delete.php" method="post">
            <input type="hidden" name="id" value="<?=$type['id']?>" />
            <button class="btn btn-danger">delete</button>
          </form>
        </td>
    </tr>

    <?php
        endforeach
    ?>

  </table>

<?php
  require_once("foot.php");
?>

如果可能的话并回答解释,我是新手。先感谢您!

标签: phpandroidhtml

解决方案


您可以简单地使用 Webview 并像这样加载您的网址

<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://www.example.com");

推荐阅读