Intent aus CustomImageView erstellen

A

AlexL1980

Neues Mitglied
0
Hallo!

Ich habe eine CustomImageView erstellt:

Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Landkarte [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Activity {[/SIZE]
 
[LEFT][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]/** Called when the activity is first created. */[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onCreate(Bundle savedInstanceState) {[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].onCreate(savedInstanceState);[/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//setContentView(R.layout.landkarte);[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
 
 
[SIZE=2]CustomImageView myView = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] CustomImageView(getApplicationContext()); [/SIZE]
[SIZE=2]System.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2].println([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Setting the view"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]); [/SIZE]
[SIZE=2]myView.invalidate(); [/SIZE]
[SIZE=2]setContentView(myView); [/SIZE]
 
[LEFT][SIZE=2]System.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2].println([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Calling invalidate"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]); [/SIZE][/LEFT]
 
 
[LEFT][SIZE=2]}[/SIZE][/LEFT]
[SIZE=2]}[/SIZE]

Das funktioniert sehr gut. Habe eine eigene ondraw-Funktion geschrieben. Jetzt möchte ich aber auf Benutzereingaben reagieren und bei einem Klick auf einen bestimmten Bereich auf dem Bildschirm einen neuen Intent starten:​

Code:
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] CustomImageView [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ImageView[/SIZE][/LEFT]
 
[LEFT][SIZE=2]{ [/SIZE][/LEFT]
 
 
[LEFT][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][B]public[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] CustomImageView(Context context) [/SIZE][/LEFT]
 
[LEFT][SIZE=2]{ [/SIZE][/LEFT]
 [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](context); [/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// [/COLOR][/SIZE][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f9fbf][SIZE=2][COLOR=#7f9fbf]TODO[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f] Auto-generated constructor stub [/COLOR][/SIZE][/COLOR][/SIZE]
 
[LEFT][SIZE=2]setBackgroundColor(0xffffffff);[/SIZE][/LEFT]
 
[LEFT][SIZE=2]} [/SIZE][/LEFT]
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] CustomImageView(Context context, AttributeSet attrs) [/SIZE][/LEFT]
 
[LEFT][SIZE=2]{ [/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](context, attrs); [/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// [/COLOR][/SIZE][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f9fbf][SIZE=2][COLOR=#7f9fbf]TODO[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f] Auto-generated constructor stub [/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
 [SIZE=2]} [/SIZE]
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] CustomImageView(Context context, AttributeSet attrs, [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] defStyle) [/SIZE][/LEFT]
 
[LEFT][SIZE=2]{ [/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](context, attrs, defStyle); [/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// [/COLOR][/SIZE][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f9fbf][SIZE=2][COLOR=#7f9fbf]TODO[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f] Auto-generated constructor stub [/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
 
[LEFT][SIZE=2]} [/SIZE][/LEFT]
 
 
 
[SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]protected [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onDraw(Canvas canvas) [/SIZE][/LEFT]
 
[LEFT][SIZE=2]{ [/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// [/COLOR][/SIZE][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f9fbf][SIZE=2][COLOR=#7f9fbf]TODO[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f] Auto-generated method stub [/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].onDraw(canvas); [/SIZE]
[SIZE=2]....[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
 
 
[LEFT][SIZE=2][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE][/SIZE][/LEFT]
 
[LEFT][SIZE=2][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]boolean[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onTouchEvent(MotionEvent event) [/SIZE][/SIZE][SIZE=2][SIZE=2]{ [/SIZE][/SIZE][/LEFT]
 
[LEFT][SIZE=2][SIZE=2][SIZE=2]Intent intent = [/SIZE][/SIZE][/SIZE][SIZE=2][SIZE=2][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Intent([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]CustomImageView.this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2], Hauptscreen.[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]); [/SIZE][/SIZE][/SIZE][SIZE=2][/LEFT]
[SIZE=2][SIZE=2][U]startActivity[/U](intent);[/SIZE]
[SIZE=2][U]finish[/U]();[/SIZE]
[LEFT]}[/LEFT]
[/SIZE][/SIZE]


Leider funktioniert das nicht. Die Meldung besagt: The constructor Intent(CustomImageView, class<Hauptscreen>) is undefinied.​

Das liegt bestimmt daran, dass diese Klasse kein extends Activity sondern extends ImageView enthält... aber wie kann ich mir da helfen? Ich verstehe noch nicht ganz, wie die intents untereinander agieren können... wie komme ich aus der CustomImageView wieder raus um von der Landkarte class neue intents zu starten?​

Vielen Dank!​

Gruß,​

Alex​
 
Zuletzt bearbeitet:
Hi,

stimmt, deshalb musst Du auch in Deinem Intent das Context verwenden, dass Du bei der Erzeigung der Custom View erhälst.

Viele Grüße

Luser_k

Gesendet mit der Android-Hilfe.de-App
 
Hallo Luser_k!

Danke für Deine Antwort. Ich verstehe sie nur nicht... hast Du ein Beispiel? Das Problem ist, dass der Compiler das intent gar nicht im ontoucheventhandler kennt.

Intent intent =​
new Intent(getContext(), Hauptscreen.class);

startActivity(intent);

finish();

The method startActivity(Intent) is undefined for the type CustomImageView
The method finish() is undefined for the type CustomImageView
 
Man sollte auch kein Intent aus dem View feuern. Warum feuerst du kein Event aus dem View und reagierst darauf in der Activity?
 
Weil dies mein erster custom view ist und ich deshalb wenig Ahnung davon habe. ;)

Wie kann ich aus der View (ontouchevent) mit der Activity kommunizieren?

Gesendet mit der Android-Hilfe.de-App
 
So wie bei anderen Views auch mit Listenern.
 
Also in der activity setonclicklistener? Kommt das dann nicht mit dem ontouchevent der view ins Gehege?
 
Hänge doch einfach einen OnTouchListener an deinen View an.
 
Dann hab ich aber wieder das Problem, dass die View das startActivity() nicht kennt, oder?

Ein Beispiel würde mir sehr helfen.
 
Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Landkarte [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Activity {[/SIZE]
 
[LEFT][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]/** Called when the activity is first created. */[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onCreate(Bundle savedInstanceState) {[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].onCreate(savedInstanceState);[/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//setContentView(R.layout.landkarte);[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
 
 
[SIZE=2]CustomImageView myView = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] CustomImageView(getApplicationContext()); [/SIZE]
[SIZE=2]System.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2].println([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Setting the view"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]); [/SIZE]
[SIZE=2]myView.invalidate(); [/SIZE]
[SIZE=2]setContentView(myView); [/SIZE]
 myView.setOnTouchListener(new OnTouchListener() {
public void onTouch() {
//startActivity
});
[LEFT][SIZE=2]System.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]out[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2].println([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Calling invalidate"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]); [/SIZE][/LEFT]
 
 
[LEFT][SIZE=2]}[/SIZE][/LEFT]
[SIZE=2]}[/SIZE]

Mal eben kurz ins unreine geschrieben.
 
  • Danke
Reaktionen: AlexL1980
Vielen Dank! Es hat geklappt. Diese ontouch-Funktion hatte ich vorher im View stehen... deshalb ging es nicht bzw. konnte nicht auf die Activity zugreifen.

Du hast mir sehr geholfen!
 

Ähnliche Themen

5
Antworten
0
Aufrufe
1.141
586920
5
R
Antworten
6
Aufrufe
994
swa00
swa00
W
Antworten
2
Aufrufe
739
rene3006
R
Zurück
Oben Unten