Nie jesteś zalogowany.
Jeśli nie posiadasz konta, zarejestruj je już teraz! Pozwoli Ci ono w pełni korzystać z naszego serwisu. Spamerom dziękujemy!
Prosimy o pomoc dla małej Julki — przekaż 1% podatku na Fundacji Dzieciom zdazyć z Pomocą.
Więcej informacji na dug.net.pl/pomagamy/.



Użytkownik
potrzebuję napisać mała aplikację dla Androida, ma to pobierać dane z lokalnie dostępnego serwera www (te dane takie już suche do zaprezentowania) i jakoś sprytnie pokazać i pozwolić je zmodyfikować
co mam:
1. zapytania http działają
2. nie mogę zmienić koloru elementów w programie
poniżej kod i pytanie: co jest źle że metoda "setColor" dla TextView oraz Button od razu wywala program
Android 4.4 - bo pewnie ma to znaczenie
a najprościej byłoby mi to dynamicznie pokolorować, coś na biało coś na zielono, najlepiej buttonom background dać kolorowy... ale ni ciuja nie działa :(
a że to mój pierwszy program w javie to i nie bardzo mam jakieś doświadczenie :-/
sterowanie.java:
package com.example.sterowanie;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Sterowanie_kosmosem extends Activity {
private Button button1;
private TextView napis1, komunikat;
private String pobrane = "";
private String akcja = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sterowanie_kosmosem);
StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
addListenerOnButton1();
addListenerOnNapis1();
}
private String pobierzHTML(String adres) {
String wyjscie = "";
try {
URL url = new URL(adres);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
wyjscie = readStream(con.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
return wyjscie;
}
private String readStream(InputStream in) {
String wyjscie = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
wyjscie += line;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return wyjscie;
}
public void przelacz(String modul, String co, String akcja) {
button1 = (Button) findViewById(R.id.button1);
pobrane=pobierzHTML("http://127.0.0.1:1080/01/status.php");
komunikat.setText(pobrane);
String[] rows = pobrane.split(";");
String[][] urzadzenie = new String[rows.length][];
int r = 0;
for (String row : rows) {
urzadzenie[r++] = row.split("~");
}
for (String row : rows) {
urzadzenie[r++] = row.split("~");
}
if (urzadzenie[0][1]=="1") {
napis1.setText(urzadzenie[0][0]+" - '+'");
napis1.setTextColor(0x000000);
} else {
napis1.setText(urzadzenie[0][0]+" - '-'");
napis1.setTextColor(0x00FF00);
}
}
public void addListenerOnButton1() {
button1 = (Button) findViewById(R.id.button1);
napis1 = (TextView) findViewById(R.id.napis1);
komunikat = (TextView) findViewById(R.id.komunikat);
akcja = "1";
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
przelacz("0001", "0001", akcja);
}
});
}
public void addListenerOnNapis1() {
napis1 = (TextView) findViewById(R.id.napis1);
komunikat = (TextView) findViewById(R.id.komunikat);
akcja = "1";
napis1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
przelacz("0001", "0001", akcja);
}
});
}
}oraz activity_sterowanie.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="Button" />
<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:text="Button" />
<Button
android:id="@+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button3"
android:text="Button" />
<Button
android:id="@+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button5"
android:layout_below="@+id/button5"
android:text="Button" />
<TextView
android:id="@+id/napis1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toLeftOf="@+id/button1"
android:text="TextView" />
<TextView
android:id="@+id/komunikat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="64dp"
android:layout_marginLeft="108dp"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="Button" />
<Button
android:id="@+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button4"
android:layout_below="@+id/button4"
android:text="Button" />
</RelativeLayout>Offline



Administrator




Nie znam się, ale chyba wartość koloru nie teges. Tutaj co nieco o kolorach - http://developer.android.com/reference/android/graphics/Color.html .
Offline



Użytkownik




Zbanowany





1869
Ostatnio edytowany przez uzytkownikubunt (2016-12-01 01:16:23)
Offline



Użytkownik