android app小白试水6-button&toast

来源:互联网 发布:python爬虫怎样赚外快 编辑:程序博客网 时间:2024/05/28 18:44

这一节其实是要做一个手机端的表单,供用户进行填写。最后的成品应该是这样的:


由于是做成竖版的排版,因此可以直接将fragment_main.xml中的标签RelativeLayout 改成<LinearLayout>并且指明是竖版排版:

android:orientation="vertical" 
然后从控件区域添加不同的text控件。注意由于不同的text控件会出现不同的输入法键盘,因此需要添加不同类型的text控件。每一个控件可以添加hint来标识提示性的文字:

android:hint="a simple password"

fragment_main.xml的代码如下:

<EditText        android:id="@+id/name"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ems="10"        android:inputType="textPersonName"         android:hint="@string/your_full_name">]        <requestFocus />    </EditText>    <EditText        android:id="@+id/password"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ems="10"        android:inputType="textPassword"         android:hint="a simple password"/>    <EditText        android:id="@+id/phone"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ems="10"        android:inputType="phone"         android:hint="your phone number"/>    <EditText        android:id="@+id/email"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ems="10"        android:inputType="textEmailAddress"         android:hint="your email address"/>    <ImageButton        android:id="@+id/survey"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="formProcess"        android:src="@drawable/ic_launcher"         />
注意在其中为每个ID添加了可读性更强的文字,另外还添加了imageButton以便提交用户填写的信息。


接下来便是为imagebutton添加onclick动作。每次涉及到前端和后台交互时由于使用fragment都会出现bug,因为老师在不使用fragment的时候的确方便简单很多。

像老师一样在onCreate中添加了onclick的代码:

public void formProcess(View submit){        submit.setVisibility(View.INVISIBLE);}

后来运行果然出现了bug啊!妥妥的:


后来了解到在fragment使用进行交互时要使用监听模式,并且交互代码要写在fragment的代码段中进行交互:

于是在onCreateView中添加如下代码:

ImageButton surveyB = (ImageButton)rootView.findViewById(R.id.survey);OnClickListener listener0 = null;        listener0 = new OnClickListener()        {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubv.setVisibility(View.INVISIBLE);}        };        surveyB.setOnClickListener(listener0);return rootView;


果然能够顺利的实现功能了呢~

最后添加了Toast:

Toast.makeText(v.getContext(), R.string.your_full_name,Toast.LENGTH_LONG).show();



0 0
原创粉丝点击