前言
我把AndroidStudio学习的多个代码融合到一篇里,跑肯定不行,但是这样可以一次对比哪个部分可以写神魔,不同方法区别在哪。
package com.example.myapplication
import androidx.support.v7.app.A
import android.os.Bundle
import android.view.View
import android.widget.TextView
public class MainActivity extends AppCompatActivity
implements View.OnClickListener##点击监听器的一种实现方法,通过关键字implements声明自己使用一个或者多个接口,与后面出现的带有(¥)为一组OnCheckedChangeListener(变化监听)
implements AdapterView.OnItemClickListener##listview监听{
int[] IdArray = {R.id.checkBox,R.id.checkBox2,R.id.checkBox3,R.id.checkBox4};##把多个复选按钮放在一起
TextView txv;##定义文本框
EditText txName, txphoneNum;##定义文本显示框
int Size;##定义整型变量
Button huifu;##定义按钮
RadioGroup rgp;##定义单选按钮
Button btnBuy;##定义按钮(¥)
Spinner spnFood;##下拉列表框##后附entries方式传入参数的xml文件
ListView kuang;##垂直滚动下拉列表(可以复选)##后附xml
ArrayList<String> allSelected=new ArrayList<>();
String[] Questions = {##通过后台方式传入listview选项(*)为一组
"什么门永远关不上",//0
"什么东西没人爱吃",//1
"什么瓜不能吃",
"什么布切不断",
"什么鼠最干净",
"偷什么不犯法"
};
String[] Answers = {##(*)
"球门",//0
"亏",//1
"傻瓜",
"瀑布",
"环保署",
"偷笑"
};
ListView lsvQuestions;##(*)
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txName = (EditText) findViewById(R.id.edtName);找到文本显示框id
txphoneNum = (EditText) findViewById(R.id.editphoneNum);##
txv = findViewById(R.id.helloworld);##找到文本框id,建立前后台联系
huifu=findViewById(R.id.huifu);##按钮id
rgp = findViewById(R.id.rgpChoose);##单选按钮只需找到一整组的id
Size = 30;设置初始尺寸
btnBuy.setOnClickListener(this);//申请监听(¥)
spnFood = findViewById(R.id.spnFood);##找到下拉菜单id
kuang=(ListView)findViewById(R.id.kuang);#listview后几句为和他一组的设置
wenben=(TextView)findViewById(R.id.wenben);
kuang.setOnItemClickListener(this);##谁被监听
String A="kong";
wenben.setText(A);
ArrayAdapter<String> Adapter = new ArrayAdapter<>##(*)(this,android.R.layout.simple_gallery_item,Questions);
lsvQuestions = findViewById(R.id.lsvQuestions);##(*)
lsvQuestions.setAdapter(Adapter);##(*)
}
public void bigger(View v)##以onclick(点击)方式为激活条件(建立前后台联系
)
{
txv.setTextSize(++Size);##文本框尺寸变大
}
public void Show(View v)
{
txInfor.setText(txName.getText() + "的电话号码:" + txphoneNum.getText());
##设置文本内容
}
huifu.setOnClickListener(new View.OnClickListener() {##匿名类方式实现按键监听器
@Override
public void onClick(View v) {
txvHello.setText(name.getText()+"电话号码是"+phone.getText());
}
});
txvHello.setOnLongClickListener(new View.OnLongClickListener() {##匿名类方式实现长按监听器
@Override
public boolean onLongClick(View v) {
txvHello.setTextSize(30);
Size=30;
return false;
}
});
btnBuy.setOnClickListener(new View.OnClickListener() {##匿名类的方式实现单选按钮单击触发监听器
@Override
public void onClick(View v) {##当单机发生用条件语句if判断触者
if(rgp.getCheckedRadioButtonId() == R.id.rbtLR)
{
txvIfno.setText("您已选择 老人 票.");
}else if(rgp.getCheckedRadioButtonId() == R.id.rbtCR)
{
txvIfno.setText("您已选择 成人 票.");
}else if(rgp.getCheckedRadioButtonId() == R.id.rbtET)
{
txvIfno.setText("您已选择 儿童 票.");
}else
{
txvIfno.setText("没有选择任何种类的票.");
}
}
});
@Override##监听器(¥)
public void onClick(View v) {##public void onCheckedChanged(RadioGroup group, int checkedId)变化监听的话是这样
if(rgp.getCheckedRadioButtonId() == R.id.rbtLR)
{
txvIfno.setText("您已选择 老人 票.");
}else if(rgp.getCheckedRadioButtonId() == R.id.rbtCR)
{
txvIfno.setText("您已选择 成人 票.");
}else if(rgp.getCheckedRadioButtonId() == R.id.rbtET)
{
txvIfno.setText("您已选择 儿童 票.");
}else
{
txvIfno.setText("没有选择任何种类的票.");
}
}
@Override##匿名类监听器
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnBuy = findViewById(R.id.btnBuy);
btnBuy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox chkTemp;##定义复选按钮
String MsgInfo = "";
TextView txvInfo;
txvInfo = findViewById(R.id.txvInfo);
for(int id:IdArray)##for循环传入复选按钮
{
chkTemp = findViewById(id);
if(chkTemp.isChecked())
{
MsgInfo = MsgInfo + chkTemp.getText();
}
}
txvInfo.setText(MsgInfo);
}
});
}
btnEat.setOnClickListener(new View.OnClickListener() {##匿名类下拉菜单
@Override
public void onClick(View v) {
String[] AllFood = getResources().getStringArray(R.array.Food);
int Index = spnFood.getSelectedItemPosition();##获取文本
txvInfomation.setText("今天吃 " + AllFood[Index]);
}
###另外一种
##@Override
##public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
## String[] AllFood = getResources().getStringArray(R.array.Food);
## txvInfomation.setText("今天吃 " + AllFood[position]);
}
##@Override
## public void onNothingSelected(AdapterView<?> parent) {
}
});
@Override##listview选择监听
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String[] mifan={"烤肉拌饭","脆皮鸡米饭"};
String[] fangbianmian={"红烧牛肉","小鸡炖蘑菇"};
String[] fan;
if(position==0)
{
fan=mifan;
}else
{
fan=fangbianmian;
}
ArrayAdapter<String> bianhua= new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,fan);####更新选项
kuang.setAdapter(bianhua);
}
lsvQuestions.setOnItemClickListener(new AdapterView.OnItemClickListener() {##匿名类后台listview监听(*)
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast tst =Toast.makeText(getApplicationContext(), "答案:" + Answers[position], Toast.LENGTH_SHORT);##问题和答案的一一映射
tst.show();
}
});
}
<resources>##spinner xml
<string name="app_name">My Application</string>
<string-array name="Food">
<item >米饭</item>
<item >油条</item>
<item >面条</item>
<item >咸菜</item>
</string-array>
</resources>
<resources>##listview xml
<string name="app_name">My Application</string>
<string-array name="Food">
<item >米饭</item>
<item >面条</item>
</string-array>
</resources>
package com.example.myapplication;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
implements DialogInterface.OnClickListener##对话框监听 {
TextView InfoShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InfoShow=findViewById(R.id.i);
AlertDialog.Builder Dlg=new AlertDialog.Builder(this);
Dlg
.setMessage("你喜欢研究生吗")##对话框问题
.setNegativeButton("不喜欢",this)##对话框选项
.setPositiveButton("喜欢",this)
.show();
}
@Override
public void onClick(DialogInterface dialog, int which) {
if(which==DialogInterface.BUTTON_POSITIVE){InfoShow.setText("我也是");}
else if (which==DialogInterface.BUTTON_NEGATIVE){InfoShow.setText("那就强迫自己喜欢");}
else{InfoShow.setText("慢慢你会喜欢的");}
}
}
两个界面交互
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
TextView daan1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
daan1=findViewById(R.id.textView);
}
public void dian2(View v){
Intent it=new Intent();
it.putExtra("haha","haha");##传入下一个界面的
setResult(RESULT_OK,it);
finish();
}
}
package com.example.myapplication;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tx;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tx=findViewById(R.id.textView2);
}
public void baidu(View v){
Intent it=new Intent();
it.setAction(Intent.ACTION_VIEW);##跳转类型
Uri uri=Uri.parse("https://www.baidu.com");##跳转网址
it.setData(uri);
startActivity(it);
}
public void dian(View v){
Intent it=new Intent();
it.setClass(this,Main2Activity.class);
startActivityForResult(it,100);##编码
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(RESULT_OK==resultCode)
{ if(100==requestCode){
tx.setText(data.getStringExtra("haha"));
}
}
}
}