ITI0011:Android praktikum 1
23. praktikumis tehtud kood: Meedia:ITI0011-Android-FirstApp.zip
Põhilised koodifailid
MainActivity.java
<source lang="java"> package ee.ttu.firstapp;
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView;
public class MainActivity extends Activity {
private EditText questionField; private Button askButton; private TextView responseText; private ImageView responseImage;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
Log.d("MainActivity", "ON CREATE");
setContentView(R.layout.activity_main);
questionField = (EditText) findViewById(R.id.questionField); askButton = (Button) findViewById(R.id.askButton); responseText = (TextView) findViewById(R.id.responseText); responseImage = (ImageView) findViewById(R.id.responseImage); }
public void ask(View view) { String question = questionField.getText().toString();
Log.d("MainActivity", "Question: " + question);
if (Math.random() < 0.5) { responseText.setText("Nope!"); responseImage.setImageResource(R.drawable.grumpycat_nope); } else { responseText.setText("Hell yeah!"); responseImage.setImageResource(R.drawable.thumbs_up); } }
public void showSecond(View view) { Intent intent = new Intent(this, SecondActivity.class);
String question = questionField.getText().toString();
intent.putExtra("küsimus", question);
startActivity(intent); }
}
</source>
SecondActivity.java
<source lang="java"> package ee.ttu.firstapp;
import android.app.Activity; import android.os.Bundle; import android.widget.TextView;
public class SecondActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView placeholder = (TextView) findViewById(R.id.placeholder); String question = getIntent().getStringExtra("küsimus");
placeholder.setText(question); } }
</source>
AndroidManifest.xml
<source lang="xml"> <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ee.ttu.firstapp" >
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SecondActivity"/> </application>
</manifest>
</source>