<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" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="35dp" android:text="Click" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" android:layout_marginTop="51dp" android:text="result" /> </RelativeLayout>
package com.myapp; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.RatingBar; import android.widget.TextView; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; public class MainActivity extends Activity { public TextView txtView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtView = (TextView)findViewById(R.id.textView1); // TextView // Button1 final Button btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { ShowDialog(); } }); } public void ShowDialog() { final AlertDialog.Builder popDialog = new AlertDialog.Builder(this); final RatingBar rating = new RatingBar(this); rating.setMax(6); popDialog.setIcon(android.R.drawable.btn_star_big_on); popDialog.setTitle("Vote!! "); popDialog.setView(rating); // Button OK popDialog.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { txtView.setText(String.valueOf(rating.getProgress())); dialog.dismiss(); } }) // Button Cancel .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); popDialog.create(); popDialog.show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท