Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Android Java: Event zur Laufzeit erstellen und einem View-Element zuordnen

26.09.2018 (👁197)

Android Java: View Event zur Laufzeit erstellen

1.Variante

Separate Definition

Den Event mit new View.OnClickListener erstellen

View.OnClickListener on_Button_clicked = new View.OnClickListener() {
   
public void onClick(View v) {
        show_Messagebox(
"event View.OnClickListner on_Button_clicked", "event clicked");
    }
};

 

Und dann im Java Code einem neuen Element zuordnen

button1.setOnClickListener(on_Button_clicked);

 

 

2.Variante

Inline Definition

button2.setOnClickListener(new View.OnClickListener() {
   
@Override
   
public void onClick(View v) {
        show_Messagebox(
"new View.OnClickLIsterner()", "onClick_event");
    }
});

Komplettes Code-Beispiel

MainActivity.java unter app/java

Android Java

package com.codedocu.myapplication;

import android.app.AlertDialog;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity  {
   
//----------------------< Activity >----------------------

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


   
public void create_Buttons()
    {
       
//------------< create_Buttons() >------------
        //< get_Layout >
       
LinearLayout root_Panel= findViewById(R.id.root_Layout);
       
//< get_Layout >

        //--< Element1 >--
       
Button button1=new Button(this);
        button1.setText(
"Button 1");
        button1.setTag(
"Button1");  //*=name
       
button1.setBackgroundColor(Color.YELLOW);
        button1.setOnClickListener(
on_Button_clicked);

       
//< add >
        
root_Panel.addView(button1);
       
//</ add >
        //--</ Element1 >--

        //--< Element2 >--
        //*Inline-Event
       
Button button2=new Button(this);
        button2.setText(
"Button 2");
        button2.setTag(
"Button2");
        button2.setBackgroundColor(Color.
RED);
        button2.setOnClickListener(
new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
                show_Messagebox(
"new View.OnClickLIsterner()", "onClick_event");
            }
        });

       
//< add >
       
root_Panel.addView(button2);
       
//</ add >
        //--</ Element1 >--


        //------------</ create_Buttons() >------------
   
}


   
//===================< region: events >===================
   
View.OnClickListener on_Button_clicked = new View.OnClickListener() {
       
public void onClick(View v) {
            show_Messagebox(
"event View.OnClickListner on_Button_clicked", "event clicked");
        }
    };
   
//===================</ region: events >===================


    //============< region: system >==============
   
public void show_Messagebox(CharSequence  Message_Text, CharSequence Title) {
       
//--------------< fp_Messagebox() >----------------
        //< Message erstellen >
        
AlertDialog.Builder msg = new AlertDialog.Builder(this);
        msg.setTitle(Title);
        msg.setMessage(Message_Text);
        msg.setCancelable(
true);
       
//</ Message erstellen >

        //< anzeigen >
       
AlertDialog msgDialog = msg.create();
        msgDialog.show();
       
//</ anzeigen >
        //--------------</ fp_Messagebox() >----------------
   
}
   
//=============</ region: system >=============
    //===================</ Activity >======================
}

 

 

 

 

Die dazugehörende Activity

Unter app/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
android:id="@+id/root_Layout"
   
android:layout_height="match_parent"
   
android:layout_width="match_parent"
   
android:orientation="vertical"
   
>

</
LinearLayout>