activity安卓作业 3月5号
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了activity安卓作业 3月5号相关的知识,希望对你有一定的参考价值。
public class MainActivity extends AppCompatActivity { private EditText editText; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText =(EditText)findViewById(R.id.xm); textView= (TextView) findViewById(R.id.jl); findViewById(R.id.pg).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(MainActivity.this,Main2Activity.class); intent.putExtra("date",editText.getText().toString()+"同学你有信心么?"); startActivityForResult(intent,0); } }); findViewById(R.id.exit).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); textView.setText(data.getStringExtra("date")); } } 第二个java代码 public class Main2Activity extends AppCompatActivity { private TextView jl; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); jl= (TextView) findViewById(R.id.jl); Intent intent=getIntent(); jl.setText(intent.getStringExtra("date")); } public void onClick(View view){ switch (view.getId()){ case R.id.yes: back("有"); break; case R.id.no: back("没有"); break; } } public void back(String content){ Intent intent=new Intent(); intent.putExtra("date",content); setResult(1,intent); finish(); } }
<LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/pg" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/pg" /> <Button android:id="@+id/exit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/exit" /> </LinearLayout> </LinearLayout> <TextView android:id="@+id/jl" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="bottom" android:text="@string/jl" android:textColor="@color/colorPrimaryDark" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/yes" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/yes" /> <Button android:id="@+id/no" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/no" /> </LinearLayout> </LinearLayout>
以上是关于activity安卓作业 3月5号的主要内容,如果未能解决你的问题,请参考以下文章