Android studio新手综合实例

it2025-07-13  2

MainActivity

package com.example.text; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.support.annotation.IntDef; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText ename,ephone; Button toast,totwo,close; RadioButton rtb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.one); ename=(EditText)findViewById(R.id.ename); ephone=(EditText)findViewById(R.id.ephone); toast=(Button)findViewById(R.id.toast); totwo=(Button)findViewById(R.id.totwo); close=(Button)findViewById(R.id.close); rtb=(RadioButton)findViewById(R.id.radioButton); toast.setOnClickListener(new View.OnClickListener(){ //匿名内部类 @Override public void onClick(View v) { Bundle bundle=new Bundle(); Intent intent=new Intent(MainActivity.this,SecondActivity.class); //内部类里只有this指自己的 bundle.putString("name",ename.getText().toString()); bundle.putString("phone",ephone.getText().toString()); intent.putExtras(bundle); startActivity(intent); } }); SharedPreferences sp=getSharedPreferences("date",MODE_PRIVATE); ename.setText(sp.getString("name","")); ephone.setText(sp.getString("phone","")); totwo.setOnClickListener(new toTwo()); //内部类 } class toTwo implements View.OnClickListener{ //内部类 @Override public void onClick(View v) { SharedPreferences sp=getSharedPreferences("date",MODE_PRIVATE); SharedPreferences.Editor editor=sp.edit(); editor.putString("name",ename.getText().toString()); if(rtb.isChecked()){ editor.putString("phone",ephone.getText().toString()); }else{ editor.putString("phone",""); } editor.commit(); Toast.makeText(MainActivity.this, "保存成功!", Toast.LENGTH_SHORT).show(); Bundle bundle=new Bundle(); Intent intent=new Intent(MainActivity.this,SecondActivity.class); //内部类里只有this指自己的 bundle.putString("name",ename.getText().toString()); bundle.putString("phone",ephone.getText().toString()); intent.putExtras(bundle); startActivity(intent); } } public void Close(View v){ finish(); } }

SecondActivity

package com.example.text; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.EditText; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; /** * Created by Administrator on 2020/10/21. */ public class SecondActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,View.OnClickListener{ TextView text; EditText ename,ephone; ListView lv; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.two); ename=(EditText)findViewById(R.id.ename); ephone=(EditText)findViewById(R.id.ephone); lv=(ListView)findViewById(R.id.lv); lv.setAdapter(new MyAdapter()); Intent intent=getIntent(); Bundle bundle=intent.getExtras(); ename.setText(bundle.getString("name")); ephone.setText(bundle.getString("phone")); } class MyAdapter extends BaseAdapter{ @Override public int getCount() { return 20; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView tv=new TextView(SecondActivity.this); tv.setText("我是第"+position+"个条目!"); tv.setTextSize(20); tv.setTextColor(Color.BLUE); return tv; } } @Override public void onClick(View v) { //继承类重写方法 } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }

one.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="四种点击事件" android:textSize="18dp" android:gravity="center"/> </LinearLayout> <LinearLayout android:paddingTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名:"/> <EditText android:layout_width="180dp" android:layout_height="wrap_content" android:hint="姓名" android:id="@+id/ename"/> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="电话:"/> <EditText android:layout_width="180dp" android:layout_height="wrap_content" android:hint="电话" android:id="@+id/ephone"/> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:id="@+id/toast" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="totwo" /> <Button android:id="@+id/totwo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" /> </LinearLayout> </LinearLayout> <RadioButton android:id="@+id/radioButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="记住密码" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/close" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="关闭程序" android:onClick="Close"/> </LinearLayout> </LinearLayout>

two.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="姓名" android:id="@+id/ename" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="电话" android:id="@+id/ephone" /> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="修改" android:id="@+id/update"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="#999999" android:dividerHeight="3dp" android:listSelector="#55225555"> </ListView> </LinearLayout> </LinearLayout>
最新回复(0)