九宫格抽奖
package com.example.nine; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import org.w3c.dom.Text; import java.lang.reflect.Array; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //引入组件 final Button b1 = (Button)findViewById(R.id.b1); final Button b2 = (Button)findViewById(R.id.b2); final Button b3 = (Button)findViewById(R.id.b3); final Button b4 = (Button)findViewById(R.id.b4); final Button b5 = (Button)findViewById(R.id.b5); final Button b6 = (Button)findViewById(R.id.b6); final Button b7 = (Button)findViewById(R.id.b7); final Button b8 = (Button)findViewById(R.id.b8); final Button b9 = (Button)findViewById(R.id.b9); final TextView t1 = (TextView)findViewById(R.id.text); //设置监听 b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //点击事件执行,同时关闭其余按钮 t1.setText("恭喜你中奖了!"); b1.setText("100元"); b2.setClickable(false); b3.setClickable(false); b4.setClickable(false); b5.setClickable(false); b6.setClickable(false); b7.setClickable(false); b8.setClickable(false); b9.setClickable(false); } }); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { t1.setText("恭喜你中奖了!"); b1.setClickable(false); b2.setText("HUAWEI"); b3.setClickable(false); b4.setClickable(false); b5.setClickable(false); b6.setClickable(false); b7.setClickable(false); b8.setClickable(false); b9.setClickable(false); } }); b3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { t1.setText("恭喜你中奖了!"); b1.setClickable(false); b2.setClickable(false); b3.setText("xiaomi"); b4.setClickable(false); b5.setClickable(false); b6.setClickable(false); b7.setClickable(false); b8.setClickable(false); b9.setClickable(false); } }); b4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { t1.setText("恭喜你中奖了!"); b1.setClickable(false); b2.setClickable(false); b3.setClickable(false); b4.setText("1角"); b5.setClickable(false); b6.setClickable(false); b7.setClickable(false); b8.setClickable(false); b9.setClickable(false); } }); b5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { t1.setText("恭喜你中奖了!"); b1.setClickable(false); b2.setClickable(false); b3.setClickable(false); b4.setClickable(false); b5.setText("ipad"); b6.setClickable(false); b7.setClickable(false); b8.setClickable(false); b9.setClickable(false); } }); b6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { t1.setText("恭喜你中奖了!"); b1.setClickable(false); b2.setClickable(false); b3.setClickable(false); b4.setClickable(false); b5.setClickable(false); b6.setText("AKW"); b7.setClickable(false); b8.setClickable(false); b9.setClickable(false); } }); b7.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { t1.setText("恭喜你中奖了!"); b1.setClickable(false); b2.setClickable(false); b3.setClickable(false); b4.setClickable(false); b5.setClickable(false); b6.setClickable(false); b7.setText("M416"); b8.setClickable(false); b9.setClickable(false); } }); b8.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { t1.setText("很遗憾,你没有中奖!"); b1.setClickable(false); b2.setClickable(false); b3.setClickable(false); b4.setClickable(false); b5.setClickable(false); b6.setClickable(false); b7.setClickable(false); b8.setText("谢谢惠顾"); b9.setClickable(false); } }); b9.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { t1.setText("恭喜你中奖了!"); b1.setClickable(false); b2.setClickable(false); b3.setClickable(false); b4.setClickable(false); b5.setClickable(false); b6.setClickable(false); b7.setClickable(false); b8.setClickable(false); b9.setText("笑口常开"); } }); } }
代码运行截图
Component Tree
应用登陆界面代码
package com.example.homework2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button blogin = (Button)findViewById(R.id.enter);//先引用组件,然后执行算法
blogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String user = "zhangsan";
String password = "123456";
EditText u = (EditText)findViewById(R.id.etname);
EditText p = (EditText)findViewById(R.id.etpass);
if (u.getText().toString().equals(user)){//判断用户名是否正确
//读取文本信息,转为字符串,判断正确
if (p.getText().toString().equals(password)){
//消息提示
Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_LONG).show();
}else{
//密码输入错误
Toast.makeText(MainActivity.this, "密码错误", Toast.LENGTH_LONG).show();
}
}else{
//用户名错误提示
Toast.makeText(MainActivity.this, "用户名错误", Toast.LENGTH_LONG).show();
}
}
});
}
}
代码未实现记住密码与忘记密码选项,初始用户名为:zhangsan,初始密码为:123456.
代码运行截图
Component Tree 截图