Android EditText值不去onclick节
我对android还是很陌生。 我已经做了很多html、php、visual basic和一些C语言的编码,但Java/Android对我来说仍然是一个初级阶段 当我运行下面的代码并尝试验证box_email EditText字段的语法时,我得到一个空值,它会使emulator崩溃。如果我传递一个默认字符串,比如lll@aol.com它有效,如果我通过它也有效lll@blah. 它正确地标识了有效和无效语法,但看不到从表单传递的任何数据Android EditText值不去onclick节,android,android-edittext,Android,Android Edittext,我对android还是很陌生。 我已经做了很多html、php、visual basic和一些C语言的编码,但Java/Android对我来说仍然是一个初级阶段 当我运行下面的代码并尝试验证box_email EditText字段的语法时,我得到一个空值,它会使emulator崩溃。如果我传递一个默认字符串,比如lll@aol.com它有效,如果我通过它也有效lll@blah. 它正确地标识了有效和无效语法,但看不到从表单传递的任何数据 package test.com; im
package test.com;
import java.util.regex.Pattern;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
public class testActivity extends Activity {
int MediaState;
ImageView animation;
String EMAILTEXT;
EditText box_email, box_password, box_username;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Eula.show(this);
IntroMedia(0);
animation = (ImageView)findViewById(R.id.imageAnimation);
animation.setBackgroundResource(R.drawable.fpanim); // the frame-by-frame animation defined as a xml file within the drawable folder
Button d;
(d = (Button)findViewById(R.id.next1button)).setBackgroundDrawable(this.getResources().getDrawable(R.drawable.next));
}
public void myClickHandler1(View view) {
setContentView(R.layout.credentials);
IntroMedia(1);
Spinner spinner = (Spinner) findViewById(R.id.box_state);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.states_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
EditText box_email = (EditText) findViewById(R.id.box_email);
//Button mainNext = (Button) findViewById(R.id.nextScreenMain);
//Filter Username
EditText box_username = (EditText) findViewById(R.id.box_username);
box_username.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
String filtered_str = s.toString();
if (filtered_str.matches(".*[^a-z^0-9^A-Z].*")) {
filtered_str = filtered_str.replaceAll("[^a-z^0-9^A-Z]", "");
s.clear();
s.append(filtered_str);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
//End Filter Username
//Filter Password
EditText box_password = (EditText) findViewById(R.id.box_password);
box_password.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
String filtered_str = s.toString();
if (filtered_str.matches(".*[^a-z^0-9^A-Z].*")) {
filtered_str = filtered_str.replaceAll("[^a-z^0-9^A-Z]", "");
s.clear();
s.append(filtered_str);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
//End Filter Password
//Filter Email
box_email.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
String filtered_str = s.toString();
if (filtered_str.matches(".*[^a-z^0-9^A-Z.@_/-].*")) {
filtered_str = filtered_str.replaceAll("[^a-z^0-9^A-Z.@_/-]", "");
s.clear();
s.append(filtered_str);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
//End Filter Email
Button cred_submit;
cred_submit = (Button)findViewById(R.id.cred_submit);
cred_submit.setOnClickListener(myClickHandler_Cred_Submit);
}
private OnClickListener myClickHandler_Cred_Submit = new OnClickListener()
{
public void onClick(View v)
{
IntroMedia(1);
EMAILTEXT = box_email.getText().toString();
//Validate Email Pattern
if (checkEmail(EMAILTEXT)){
//is valid
setContentView(R.layout.validinput);
} else {
//is not
setContentView(R.layout.invalidinput);
}
//End Validate EMail Pattern
//process & validate data
//submit data to website
//enter data into prefs
}
};
public void myClickHandler_Cred_Cancel(View view) {
finish();
}
@Override
/**
* This method is called whenever the Activity becomes visible or invisible to the user.
* During this method call its possible to start the animation.
*/
public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
AnimationDrawable frameAnimation =
(AnimationDrawable) animation.getBackground();
if(hasFocus) {
frameAnimation.start();
} else {
frameAnimation.stop();
}
}
public void IntroMedia(int MediaState) {
MediaPlayer mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.smallmusicfile);
if (MediaState == 1){
mPlayer.pause();
mPlayer.seekTo(mPlayer.getDuration());
mPlayer.stop();
mPlayer.release();
}
if (MediaState == 0){
mPlayer.start();
MediaState = 1;
}
}
public final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + "\\@" + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + "(" + "\\." + "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" + ")+");
private boolean checkEmail(String email) {return EMAIL_ADDRESS_PATTERN.matcher(email).matches(); }
}
package test.com;
导入java.util.regex.Pattern;
导入android.app.Activity;
导入android.graphics.drawable.AnimationDrawable;
导入android.media.MediaPlayer;
导入android.os.Bundle;
导入android.text.Editable;
导入android.text.TextWatcher;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ImageView;
导入android.widget.Spinner;
公共类测试活动扩展了活动{
中间国家;
图像视图动画;
字符串文本;
编辑文本框\电子邮件、框\密码、框\用户名;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Eula.show(这个);
内含子(0);
动画=(ImageView)findViewById(R.id.imageAnimation);
animation.setBackgroundResource(R.drawable.fpanim);//在drawable文件夹中定义为xml文件的逐帧动画
按钮d;
(d=(Button)findViewById(R.id.next1button)).setBackgroundDrawable(this.getResources().getDrawable(R.drawable.next));
}
公共作废myClickHandler1(视图){
setContentView(R.layout.credentials);
内含子(1);
微调器微调器=(微调器)findViewById(R.id.box\u状态);
ArrayAdapter=ArrayAdapter.createFromResource(这个,R.array.states\u数组,android.R.layout.simple\u微调器\u项);
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
旋转器。设置适配器(适配器);
EditText-box\u-email=(EditText)findViewById(R.id.box\u-email);
//按钮mainNext=(按钮)findViewById(R.id.nextScreenMain);
//过滤用户名
EditText-box\u用户名=(EditText)findViewById(R.id.box\u用户名);
框_username.addTextChangedListener(新的TextWatcher(){
公共无效后文本已更改(可编辑){
字符串已筛选_str=s.toString();
如果(筛选的字符串匹配(“.[^a-z^0-9^a-z].*)){
filtered_str=filtered_str.replaceAll(“[^a-z^0-9^a-z]”,即“);
s、 清除();
s、 附加(过滤的_str);
}
}
public void beforeTextChanged(字符序列s、int start、int count、int after){}
public void onTextChanged(字符序列,int start,int before,int count){}
});
//结束筛选器用户名
//过滤密码
EditText-box\u密码=(EditText)findViewById(R.id.box\u密码);
box_password.addTextChangedListener(新的TextWatcher(){
公共无效后文本已更改(可编辑){
字符串已筛选_str=s.toString();
如果(筛选的字符串匹配(“.[^a-z^0-9^a-z].*)){
filtered_str=filtered_str.replaceAll(“[^a-z^0-9^a-z]”,即“);
s、 清除();
s、 附加(过滤的_str);
}
}
public void beforeTextChanged(字符序列s、int start、int count、int after){}
public void onTextChanged(字符序列,int start,int before,int count){}
});
//结束筛选器密码
//过滤电子邮件
box_email.addTextChangedListener(新的TextWatcher(){
公共无效后文本已更改(可编辑){
字符串已筛选_str=s.toString();
如果(筛选的字符串匹配(“.[^a-z^0-9^a-z.@/-])){
filtered_str=filtered_str.replaceAll(“[^a-z^0-9^a-z.@/-]”,即“);
s、 清除();
s、 附加(过滤的_str);
}
}
public void beforeTextChanged(字符序列s、int start、int count、int after){}
public void onTextChanged(字符序列,int start,int before,int count){}
});
//结束筛选电子邮件
按钮cred_提交;
cred_submit=(按钮)findviewbyd(R.id.cred_submit);
cred_submit.setOnClickListener(myClickHandler_cred_submit);
}
私有OnClickListener myClickHandler\u Cred\u Submit=new OnClickListener()
{
公共void onClick(视图v)
{
内含子(1);
EMAILTEXT=box_email.getText().toString();
//验证电子邮件模式
如果(检查电子邮件(电子邮件文本)){
//有效
setContentView(R.layout.validinput);
}否则{
//不是
setContentView(R.layout.invalidinput);
}
//结束验证电子邮件模式
//处理和验证数据
//向网站提交数据
//将数据输入prefs
}
};
公共作废myClickHandler\u Cred\u Cancel(查看){
完成();
}
@凌驾
/**
*这遇到了
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:id="@+id/mainLayout"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_centerVertical="true"
>
<ImageView android:id="@+id/imageAnimation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:adjustViewBounds="true"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:id="@+id/relativeLayout1"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true">
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="XXXXXXXXXXXXXXX"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"
android:layout_alignParentBottom="true"/>
<Button android:layout_height="wrap_content" android:onClick="myClickHandler1" android:layout_width="wrap_content" android:id="@+id/next1button" android:layout_above="@+id/adView" android:layout_centerHorizontal="true"></Button>
</RelativeLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:id="@+id/mainLayout"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_centerVertical="true"
>
<TextView android:text="Username: REQUIRED - Unique and Alpha Numeric " android:layout_height="wrap_content" android:id="@+id/label_username" android:layout_width="fill_parent" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"></TextView>
<EditText android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/box_username" android:layout_below="@+id/label_username" android:layout_alignParentRight="true" android:layout_alignParentLeft="true"></EditText>
<TextView android:text=" " android:layout_height="wrap_content" android:id="@+id/blank_1" android:layout_width="fill_parent" android:layout_below="@+id/box_username" android:layout_centerHorizontal="true"></TextView>
<TextView android:text="Password: REQUIRED - Alpha Numeric" android:layout_height="wrap_content" android:id="@+id/label_password" android:layout_width="fill_parent" android:layout_below="@+id/blank_1" android:layout_centerHorizontal="true"></TextView>
<EditText android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/box_password" android:layout_below="@+id/label_password" android:layout_alignParentRight="true" android:layout_alignParentLeft="true" android:inputType="textPassword"></EditText>
<TextView android:text=" " android:layout_height="wrap_content" android:id="@+id/blank_2" android:layout_width="fill_parent" android:layout_below="@+id/box_password" android:layout_centerHorizontal="true"></TextView>
<TextView android:text="Email: Optional" android:layout_height="wrap_content" android:id="@+id/label_email" android:layout_width="fill_parent" android:layout_below="@+id/blank_2" android:layout_centerHorizontal="true"></TextView>
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/box_email"
android:layout_below="@+id/label_email"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:inputType="textEmailAddress">
</EditText>
<TextView android:text=" " android:layout_height="wrap_content" android:id="@+id/blank_3" android:layout_width="fill_parent" android:layout_below="@+id/box_email" android:layout_centerHorizontal="true"></TextView>
<TextView android:text="State: Optional - If in the U.S. only." android:layout_height="wrap_content" android:id="@+id/label_state" android:layout_width="fill_parent" android:layout_below="@+id/blank_3" android:layout_centerHorizontal="true"></TextView>
<Spinner android:layout_height="wrap_content" android:id="@+id/box_state" android:layout_below="@+id/label_state" android:layout_width="fill_parent" android:prompt="@string/state_prompt" android:layout_alignParentLeft="true" android:layout_alignParentRight="true"></Spinner>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Submit" android:id="@+id/cred_submit" android:layout_below="@+id/box_state" android:layout_alignParentLeft="true" android:onClick="myClickHandler_Cred_Submit"></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Cancel" android:id="@+id/cred_cancel" android:layout_below="@+id/box_state" android:layout_alignParentRight="true" android:onClick="myClickHandler_Cred_Cancel"></Button>
<RelativeLayout
android:layout_width="fill_parent"
android:id="@+id/relativeLayout1"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true">
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="XXXXXXXXXXXXXXX"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</RelativeLayout>
EditText box_email = (EditText) findViewById(R.id.box_email);
box_email = (EditText) findViewById(R.id.box_email);