Screenshots :
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<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="com.chantisandroid.buttonexample.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clickme"
android:id="@+id/button_click"
android:layout_below="@+id/editText_enter"
android:layout_centerHorizontal="true"
android:layout_marginTop="74dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText_enter"
android:layout_marginTop="66dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="enter text here" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView_result"
android:layout_below="@+id/button_click"
android:layout_centerHorizontal="true"
android:layout_marginTop="102dp" />
</RelativeLayout>
MainActivity.java :
package com.chantisandroid.buttonexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button click;
EditText enter;
TextView results;
String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enter = (EditText)findViewById(R.id.editText_enter);
results = (TextView)findViewById(R.id.textView_result);
click = (Button)findViewById(R.id.button_click);
click.setOnClickListener(this);
}
@Override
public void onClick(View view) {
str = enter.getText().toString().trim();
results.setText(str);
}
}
No comments:
Post a Comment