Working With Widgets : Button And EditText.
Create New Project As We Made at Last artical.
in your Graphical Layout go to widget pallete and drag and drop two Edit Text And one Button.
Your XML Code Will Look Like Below.
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_centerVertical="true"
android:ems="10" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="32dp"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/editText2"
android:layout_alignLeft="@+id/editText2"
android:layout_marginBottom="38dp"
android:text="Show
Text" />
</RelativeLayout>
Now Move to your java file.
import the library needed for EditText And Button.
import android.widget.Button;
import android.widget.EditText;
Create instance Of Widgets in your activity.
EditText input , output;
Button show;
in Your On Create Method Add this code
input= (EditText) findViewById(R.id.editText1);
output= (EditText) findViewById(R.id.editText1);
show = (Button) findViewById(R.id.button1);
these are Id's of your widgets from your XML file.
Now Set OnClickListener on your show Button using below code.
show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
Now we will gettext from input and set it to output when show button is clicked.
show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String ip=input.getText().toString();
output.setText(ip);
}
});
your java file will look like below.
package arshad.widget;
import android.os.Bundle;
import
android.app.Activity;
import android.speech.tts.TextToSpeech;
import android.view.Menu;
import android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText input,output;
Button show;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input= (EditText)
findViewById(R.id.editText1);
output= (EditText)
findViewById(R.id.editText2);
show = (Button) findViewById(R.id.button1);
show.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String
ip=input.getText().toString();
output.setText(ip);
}
});
}
@Override
public boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the
menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Now Run your Application.
type something in Input and Click the show button in output field will shows input text.
this is simple tutorial to learn simple operations using Button and EditText.
All The Best :)
No comments:
Post a Comment