界面设计代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_tiaozhuan" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/holo_orange_dark" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="hello.jmtiaozhuan.TiaozhuanActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="150dp" android:hint="@string/EditTextname" android:textColorHint="#f0ff" android:gravity="center" android:textColor="#00ffee" android:id="@+id/shuru"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/button1" style="@style/text" android:onClick="onClick" android:id="@+id/button1"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/button2" style="@style/text" android:id="@+id/button2"/> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/TextViewname" android:gravity="center" android:id="@+id/show" android:textColor="#33ff00" style="@style/text" android:onClick="onClick"/> </LinearLayout> 效果如下
string文件代码
<resources> <string name="app_name">JMtiaozhuan</string> <string name="button1">点击进入评估</string> <string name="button2">退出</string> <string name="EditTextname">请输入您的姓名</string> <string name="TextViewname">输出评估结果</string> <string name="button3">有</string> <string name="button4">没有</string> </resources>重点内容 按钮监控
btbutton2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (btbutton2.isClickable()) { Toast.makeText(MainbackActivity.this, "再按一次退出按钮", Toast.LENGTH_LONG).show();//如果退出按钮被点击一下,会显示文本再按一次退出按钮 btbutton2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (btbutton2.isClickable()) { MainbackActivity.this.finish();//再一次监听按钮,如果再按一次退出按钮,就会关闭整个界面 } } }); } } });
最重要数据传递
1主界面向子界面传递数据
if (TextUtils.isEmpty(etshuru.getText().toString())) { Toast.makeText(MainbackActivity.this, "输入名称不能为空", Toast.LENGTH_SHORT).show(); //如果没有输入名字就点击进入评估的话就会显示 return; } String name = etshuru.getText().toString(); Intent mIntent = new Intent(MainbackActivity.this, JiemianActivity.class); mIntent.putExtra("name", etshuru.getText().toString().trim() + "同学:学习Android有没有信心?"); //将name字符串传递给子界面,trim()为去掉两边的空格 startActivityForResult(mIntent, 1000);
2子界面接收数据
Intent intent = getIntent(); TextView tvshow=(TextView)findViewById(R.id.show); String name=intent.getStringExtra("name"); //接收主界面传递过来的name数据 tvshow.setText(name);
3子界面返回数据
private void ruturn() { Intent intent=new Intent(this,MainbackActivity.class); Button btnbutton3=(Button)findViewById(R.id.button3); String name=btnbutton3.getText().toString(); intent.putExtra("name","评估内容返回为:"+name); finish(); }
4主界面接受返回数据
protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //使用onActivityResult(int requestCode, int resultCode, Intent data)获取data if (requestCode == 1000 && (resultCode == 1001 || resultCode == 1002)) { //使主界面的requestCode1000和子界面的resultCode1001或者1002比较 TextView tvshow = (TextView) findViewById(R.id.show); String result = data.getStringExtra("name"); //接收子界面传递的"name"数据 tvshow.setText(result); } }
时间: 2024-10-10 01:43:26