AndroidでstartActivityForResultを管理する方法は?
この例は、AndroidでstartActivityForResultを管理する方法について示しています
ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。
ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android" xmlns:app = "https://schemas.android.com/apk/res-auto" xmlns:tools = "https://schemas.android.com/tools" android:layout_width = "match_parent" android:gravity = "center" android:layout_height = "match_parent" tools:context = ".MainActivity" android:orientation = "vertical"> <TextView android:id = "@+id/actionEvent" android:textSize = "40sp" android:layout_marginTop = "30dp" android:layout_width = "wrap_content" android:layout_height = "match_parent" /> </LinearLayout>
上記のコードでは、アクティビティ結果データに表示するテキストビューを使用しています。
ステップ3 −次のコードをsrc / MainActivity.java
に追加します<?xml version = "1.0" encoding = "utf-8"?>
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView actionEvent;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionEvent = findViewById(R.id.actionEvent);
actionEvent.setText("Click");
actionEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(i, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String returnString = data.getStringExtra("result");
actionEvent.setText(returnString);
}
}
}
} ステップ3 −次のコードをsrc / SecondActivity.java
に追加します<?xml version = "1.0" encoding = "utf-8"?>
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent returnIntent = new Intent();
returnIntent.putExtra("result","data from seconActivity");
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
} ステップ3 −次のコードactivity_second.xmlを追加します
<?xml version = "1.0" encoding = "utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android = "https://schemas.android.com/apk/res/android" xmlns:app = "https://schemas.android.com/apk/res-auto" xmlns:tools = "https://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".SecondActivity"> </android.support.constraint.ConstraintLayout>
ステップ3 −次のコードAndroidManifest.xmlを追加します
<?xml version = "1.0" encoding = "utf-8"?> <manifest xmlns:android = "https://schemas.android.com/apk/res/android" package = "com.example.myapplication"> <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 = ".SecondActivity"> </activity> <activity android:name = ".MainActivity" android:configChanges = "keyboardHidden|orientation|screenSize"> <intent-filter> <action android:name = "android.intent.action.MAIN" /> <action android:name = "android.net.conn.CONNECTIVITY_CHANGE" /> <category android:name = "android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 android studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します–
次に、textviewをクリックすると、2番目のアクティビティからデータが取得され、以下に示すような結果が得られます–
-
Androidで通知を管理する方法
Androidデバイスの通知の過負荷に対処していますか?これらのアラートを1つずつチェックするのは、時間の大きな無駄です。集中力を維持するには、これらの中断をより効率的に管理する必要があります。この完全なガイドでは、最も一般的な通知の問題の解決策を紹介し、Androidで通知を管理する方法を示します。 1。 Androidシステム通知 個々のアプリが主な原因と見なされていますが、スパムの大部分の原因となっているのはAndroidシステムの通知です。ロックされていないAndroid画面には、個別のシステム通知を提供する3つの領域があります。 上部の領域にあるステータスバーのアラート 中央の
-
Android でファイルを管理する方法
最近では、スマートフォンに大容量のストレージが搭載されているため、多くのデータを Android に保存する傾向があります。私たちは管理を見落としているため、ストックされたすべてのファイルが混乱しているように見えます。電話は、必要なときに何かを見つけるために、すべてのファイルを管理する場所でなければなりません。 Androidには、設定にあるファイルマネージャーの組み込み機能が付属しています。これ以外にも、電話プロバイダーはプリインストールされたさまざまなアプリを提供しています。 Android のファイル マネージャーとは すべての電話には、そこにあるファイルを管理するためのファイル マネ