簡體   English   中英

如何將結果顯示到 TextView

[英]How to display the result into a TextView

android 開發新手,我對在布局上顯示數據感到困惑。

我正在嘗試將 url 地址打印到名為sharedText的 EditView 上

目標是通過單擊瀏覽器設置中的共享按鈕將 url 發送到我的 EditView,並在單擊共享按鈕但在應用程序上未顯示 url 地址時在我的 LogCat 中獲取此地址。

'2020-04-30 12:04:59.730 3675-3675/com.e.testshare D/MainActivity: sharedText: https://www.bbc.co.uk/news'

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

    Intent intent = getIntent();
    if (intent != null) {
        String action = intent.getAction();
        String type = intent.getType();

        if (Intent.ACTION_SEND.equals(action) && type != null) {
            if ("text/plain".equals(type)) {
                handleReceivedText(intent); // Handle text being sent
            }
        }

void handleReceivedText(Intent intent) {
    String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
    if (sharedText != null) {
        Log.d("MainActivity", "sharedText : " + sharedText);
        Intent i = new Intent(getApplicationContext(), MainActivity.class);

        i.putExtra("SHAREDTEXT", sharedText);
        startActivity(i);
    }
}

顯現:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.e.testshare">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter android:label="Share Testing">
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>
    </application>

</manifest>

布局:

<fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />

    <EditText
        android:id="@+id/sharedText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="UrlAddress"
        tools:layout_editor_absoluteX="78dp"
        tools:layout_editor_absoluteY="89dp" />
 

如果sharedText是您的EditText嘗試做sharedText.setText(sharedText) 此外,對不同的事物使用相同的變量命名也是不好的做法。 更好的是sharedTextEditTextsharedText

 void handleReceivedText(Intent intent) {
            String text= intent.getStringExtra(Intent.EXTRA_TEXT);
            if (text!= null) {
                Log.d("MainActivity", "sharedText : " + sharedText);
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                EditText sharedText = (EditText) findViewById(R.id.sharedText);
                sharedText.setText(text)
            }
        }

非常感謝

您發布的上述代碼不起作用,但將 sharedText 更改為 log.d 行上的文本

void handleReceivedText(Intent intent) {
            String text= intent.getStringExtra(Intent.EXTRA_TEXT);
            if (text!= null) {
                Log.d("MainActivity", "sharedText : " + text);
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                EditText sharedText = (EditText) findViewById(R.id.sharedText);
                sharedText.setText(text)
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM