2014年1月2日 星期四

利用AlertDialog 接收使用者輸入資料

利用AlertDialog 接收使用者輸入資料



 


自己撰寫一個利用AlertDialog 接收使用者輸入資料範例,歡迎有興趣的同好一起來C/P一下。


 


資料來源:http://androidbiancheng.blogspot.tw/


 









package com.example.framelayout_alertdialog_edittext;

import com.example.framelayout_alertdialog_edittext.MainActivity;
import com.example.framelayout_alertdialog_edittext.MainActivity.ButListener;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;

///////////////////////////////////////
import android.view.View;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.VideoView;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.view.ViewGroup.MarginLayoutParams;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
public class MainActivity extends Activity {
TextView tv_main;
Button bt_main;
FrameLayout FrameLayout1;
DisplayMetrics metrics;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/////////////////////////////////////////
requestWindowFeature(Window.FEATURE_NO_TITLE); //全螢幕設定
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
metrics = new DisplayMetrics();//抓取螢幕大小
getWindowManager().getDefaultDisplay().getMetrics(metrics);
/////////////////////////////////////////
FrameLayout1 = new FrameLayout(this);
FrameLayout.LayoutParams lparam1 = new FrameLayout.LayoutParams(metrics.widthPixels,metrics.heightPixels);
FrameLayout1.setLayoutParams(lparam1);
this.setContentView(FrameLayout1);
////////////////////////////////////////
tv_main=new TextView(this);
tv_main.setText("");
tv_main.setWidth(metrics.widthPixels/2);
tv_main.setHeight(metrics.heightPixels/2);
FrameLayout.LayoutParams layoutParams0 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2);
layoutParams0.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams0.leftMargin = 0;
layoutParams0.topMargin = 0;
tv_main.setLayoutParams(layoutParams0);
FrameLayout1.addView(tv_main);

bt_main=new Button(this);
bt_main.setText("Run PopupWindow");
bt_main.setWidth(metrics.widthPixels/2);
bt_main.setHeight(metrics.heightPixels/2);
bt_main.setOnClickListener(new ButListener());
FrameLayout.LayoutParams layoutParams1 = new FrameLayout.LayoutParams(metrics.widthPixels/2, metrics.heightPixels/2);
layoutParams1.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams1.leftMargin = metrics.widthPixels/2;
layoutParams1.topMargin = 0;
bt_main.setLayoutParams(layoutParams1);
FrameLayout1.addView(bt_main);
////////////////////////////////////////////

}

@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;
}
class ButListener implements OnClickListener
{
public void onClick(View v)
{
if(v==bt_main)
{
tv_main.setText("Create/Open AlertDialog...");
AlertDialog.Builder editDialog;
final EditText editText;
editText= new EditText(MainActivity.this);
editDialog = new AlertDialog.Builder(MainActivity.this);

editDialog.setTitle("--- Edit ---");
//final EditText editText = new EditText(MainActivity.this);
editDialog.setView(editText);
editDialog.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1)
{
tv_main.setText("OK "+editText.getText().toString());
}
});
editDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1)
{
tv_main.setText("KO "+editText.getText().toString());
}
});
editDialog.show();
}
}
}
}


 


 


沒有留言:

張貼留言