[Android] Button-Event behandeln

  1. Zuweisung der Button-onClick-Ereignisbehandlungsroutine durch das Attribut android:onClick im XML-Layout:
    <Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:text=&quot;Klick mich!&quot;
            android:id=&quot;@+id/button&quot; android:enabled=&quot;true&quot;
            android:onClick=&quot;onClick&quot;/->
    
  2. danach die Ereignisbehandlungsroutine onClick im Java-Code implementieren:
    package com.example.Hello;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    
    // Ereignisbehandlung für den Buttonklick
    public void onClick(View v)
    {
        // hier etwas machen
    }
    

[Android] Message (Toast) ausgeben

package com.example.Hello;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

/* bei Buttonklick wird eine Message angezeigt */
public void onClick(View v)
{
    Toast msg = Toast.makeText(getBaseContext(), "Dies ist eine Nachricht!", Toast.LENGTH_LONG);
    msg.show();
}

[Android] Activity fullscreen darstellen

package com.example.Hello;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setFullScreen(this);

        setContentView(R.layout.main);
    }

    protected void setFullScreen(Context ctx)
    {
        ((Activity) ctx).requestWindowFeature(Window.FEATURE_NO_TITLE);
        ((Activity) ctx).getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}

[Android] Leere Standard-Activity

package com.example.Hello;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onStart()
    {
        super.onStart();
    }

    @Override
    protected void onRestart()
    {
        super.onRestart();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }

    @Override
    protected void onPause()
    {
        super.onPause();
    }

    @Override
    protected void onStop()
    {
        super.onStop();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
    }
}