T
tomycat
Ambitioniertes Mitglied
- 0
hallo,
ich möchte gerne auf einen Button tippen und dann soll sich ein neue Fenster öffnen.
Mein Handy läuft auf Andorid 4.2.2 und mein Studio ist Eclipse.
klicke ich auf say hello, dann wird die App beendet.
die
das Layout
DAS 2 Fenster !!!!
DAS Layout
ich möchte gerne auf einen Button tippen und dann soll sich ein neue Fenster öffnen.
Mein Handy läuft auf Andorid 4.2.2 und mein Studio ist Eclipse.
klicke ich auf say hello, dann wird die App beendet.
die
Code:
public class MainActivity extends Activity
implements OnClickListener {
private Button hiButton;
private Button helloButton;
private Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hiButton = (Button) findViewById(R.id.hi_button);
hiButton.setOnClickListener(this);
helloButton = (Button) findViewById(R.id.hello_button);
helloButton.setOnClickListener(this);
}
public void onClick(View v) {
EditText nameField = (EditText) findViewById(R.id.name_field);
String name = nameField.getText().toString();
if (name.length() == 0) {
new AlertDialog.Builder(this).setMessage(
R.string.error_name_missing).setNeutralButton(
R.string.error_ok,
null).show();
return;
}
if (v == hiButton || v == helloButton) {
int resourceId = v == hiButton ? R.string.hi_greeting
: R.string.hello_greeting;
String greeting = getResources().getString(resourceId, name);
Toast.makeText(this, greeting, Toast.LENGTH_LONG).show();
TextView greetingField = (TextView) findViewById(R.id.greeting_field);
greetingField.setText(greeting);
// neu und geht net !!!!
Intent intent = new Intent(this, Activity_2.class);
this.startActivity(intent);
}
}
}
das Layout
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/greeting_field"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/enter_your_name"
/>
<EditText
android:id="@+id/name_field"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/hi_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hi_button"
android:layout_weight="1"
/>
<Button
android:id="@+id/hello_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_button"
android:layout_weight="1"
/>
<Button
android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/geheZuActivity2"
/>
</LinearLayout>
</LinearLayout>
DAS 2 Fenster !!!!
Code:
public class setup extends Activity implements OnClickListener {
private Button rechnerButton;
private Button activity1Button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setup);
activity1Button = (Button) this.findViewById(R.id.button1);
activity1Button.setOnClickListener(this);
}
public void onClick(View v) {
if(v == rechnerButton) {
Intent intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_CALCULATOR);
PackageManager pm = getPackageManager();
ComponentName cn = intent.resolveActivity(pm);
if(cn != null) {
startActivity(intent);
}
else {
Toast t = Toast.makeText(this, "Kein Taschenrechner vorhanden!", Toast.LENGTH_SHORT);
t.show();
}
}
else if(v == activity1Button) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}
}
DAS Layout
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>