Example : 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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Input Text ?" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:text="Show Toast" />
</RelativeLayout>
Example : MainActivity.java
package com.example.alertdialog2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn1 = (Button) findViewById(R.id.button1);
final TextView editT1 = (TextView)findViewById(R.id.editText1);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String strTxt = editT1.getText().toString();
Toast.makeText(MainActivity.this,
"ข้อความของคุณคือ : " + strTxt,
Toast.LENGTH_LONG).show();
}
});
}
}
ผลลัพธ์ :
ที่มา : Thaicreate
ไม่มีความคิดเห็น:
แสดงความคิดเห็น