質問

あるアクティビティ画面から別のアクティビティ画面にナビゲートする方法は?最初の画面では、ボタンをクリックすると、別のアクティビティ画面に移動する必要があるボタンが1つあります。

役に立ちましたか?

解決

OnClickListener onClickListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(action));
    }
};

Button button = (Button) findViewById(id);
button.setOnClickListener(onClickListener);

他のヒント

最も些細なケース(アクティビティから呼び出されます):

startActivity(new Intent(this, ActivityToLaunch.class));

詳細はこちら: http://developer.android.com/guide/topics/fundamentals.html

Button x.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) 
                {
                    Intent i = new Intent(y.this, Activity.class);
                    startActivity(i);                   
                }
        });

ここでは、ボタンXのリスナーを定義しました。 OSはこのメソッドを呼び出し、Intent iで参照されるアクティビティを開始します。

公式のチュートリアルの例は次のとおりです。http://developer.android.com/guide/tutorials/notepad/notepad-ex2.html

Button btn = (Button)findViewById(R.id.button1);

btn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {      

        startActivity(new Intent(TestActivity.this,second.class));

    }
});
public void onClick(View v) 
{
    Intent myintent = new Intent(currentclass.this, nextactivity.class);
    startActivity(myintent);                               
}
final Context cont = this;
Button btnClickABC =(Button)findViewById(R.id.btnClickABC);
btnClickABC.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {                   
                startActivity(new Intent(cont, NextActivity.class));

            }
        });

このタスクは、Androidのメインビルディングブロックのいずれかを意図と名付け、メソッドの1つを使用して実現できます。 public void startActivity (Intent intent) アクティビティクラスに属します。

意図は、実行される操作の抽象的な説明です。 Startactivityで使用してアクティビティを起動し、BroadCastIntentが関心のあるBroadCastReceiverコンポーネントに送信し、StartService(Intent)またはBindService(Intent、ServiceConnection、int)にバックグラウンドサービスと通信することができます。

意図は、さまざまなアプリケーションでコード間で遅延ランタイムバインディングを実行するための機能を提供します。その最も重要な用途は、アクティビティの開始であり、アクティビティ間の接着剤と考えることができます。これは基本的に、実行されるアクションの抽象的な説明を保持する受動的なデータ構造です。

公式ドキュメントを参照してください - http://developer.android.com/reference/android/content/intent.html

public void startActivity (Intent intent) - 新しいアクティビティを起動するために使用されます。

したがって、2つのアクティビティクラスがあり、ボタンクリックの上にあるとします OnClickListener() あるアクティビティから別のアクティビティに移動したい -

  1. プレゼントアクティブ - これは、2番目のアクティビティに行きたい現在のアクティビティです。

  2. NextActivity - これはあなたが移動したい次のアクティビティです。

したがって、意図はこのようなものになります

Intent(PresentActivity.this, NextActivity.class)

最後に、これは完全なコードになります

  public class PresentActivity extends Activity {
        protected void onCreate(Bundle icicle) {
            super.onCreate(icicle);

            setContentView(R.layout.content_layout_id);

            final Button button = (Button) findViewById(R.id.button_id);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Perform action on click   

                    Intent activityChangeIntent = new Intent(PresentActivity.this, NextActivity.class);

                    // currentContext.startActivity(activityChangeIntent);

                    PresentActivity.this.startActivity(activityChangeIntent);
                }
            });
        }
    }

このexmpleはボタンクリックに関連していますボタンクリックの内側に記述されているどこでもコードを使用できます OnClickListener() アクティビティを切り替えたい場所で。

次のコードを使用してください。これがあなたに役立つことを願っています。

 Button button = (Button)findViewById(R.id.xxx);
    button .setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        Intent intent=new Intent(CurrentActivity.this,NextActivity.class);
        startActivity(intent);
        }
    });

xxx ボタンのXMLからIDです。

startActivity(new Intent(this,newActivity.class));

あるアクティビティから別のアクティビティに切り替えるのは本当に簡単ですが、新しいアクティビティにとっては難しいです。次のクラスはで定義する必要があります AndroidManifest.xml. 。これはテスタークラスです:

<activity
  android:name=".Tester"
  android:label="@string/title_activity_tester" >`enter code here`
</activity>


final Button button = (Button) findViewById(R.id.btnGo);// btnGo is id 
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Intent i = new Intent(CurrentClass.this, Tester.class);
        startActivity(i);
    }

これらのコードスニペットを使用して、次の画面に移動できます。

コトリン

startActivity(Intent(this, LoginActivity::class.java))

ジャワ

startActivity(new Intent(this, LoginActivity.class))

参照は次のとおりです。 Android開発者 - 別のアクティビティを開始します

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top