美化,添加功能

This commit is contained in:
icewithcola 2024-03-26 14:28:07 +08:00
parent ebca6327e4
commit 022f9f2a3e
7 changed files with 193 additions and 75 deletions

View file

@ -2,6 +2,9 @@
<project version="4">
<component name="deploymentTargetDropDown">
<value>
<entry key="MainActivity">
<State />
</entry>
<entry key="app">
<State />
</entry>

View file

@ -6,10 +6,12 @@ import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.Manifest;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
@ -29,33 +31,42 @@ public class MainActivity extends AppCompatActivity {
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
if (Build.VERSION.SDK_INT >= 33) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 101);
}
}
}
@Override
protected void onResume(){
super.onResume();
}
private void goNextPage(){
TextView textView = findViewById(R.id.tv_hello);
textView.setText("3秒后进入下个页面");
new Handler().postDelayed(mGoNext,3000);
}
private final Runnable mGoNext = new Runnable() {
@Override
public void run() {
startActivity(new Intent(MainActivity.this,MainActivity2.class));
TextView tv = findViewById(R.id.tv_hello);
textViewAppendString(tv,"Starting Kagura Init");
if (Build.VERSION.SDK_INT >= 33) {
textViewAppendString(tv,"Find api > 33, checking permission");
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
textViewAppendString(tv,"Acquiring POST_NOTIFICATIONS");
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 101);
}
textViewAppendString(tv,"Check Finished");
}
};
textViewAppendString(tv,"*************************\nfinished");
Button button = findViewById(R.id.Page1NextPage);
button.setEnabled(true);
}
public void jumpToNext(View view) {
goNextPage();
Button button = findViewById(R.id.Page1NextPage);
button.setEnabled(true);
Intent myIntent = new Intent(this, MainActivity2.class);
startActivity(myIntent);
}
private void textViewAppendString(@NonNull TextView tv, String s){
String last = tv.getText().toString();
if (!last.endsWith("\n")){
last += "\n";
}
last += s;
tv.setText(last);
}
}

View file

@ -1,11 +1,14 @@
package uk.kagurach.android101;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
@ -18,6 +21,7 @@ import androidx.core.view.WindowInsetsCompat;
public class MainActivity2 extends AppCompatActivity {
int _text_size = 70;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -41,17 +45,17 @@ public class MainActivity2 extends AppCompatActivity {
public void set70sp(View view) {
TextView t = findViewById(R.id.test2strview);
t.setTextSize(TypedValue.COMPLEX_UNIT_SP,70);
t.setTextSize(TypedValue.COMPLEX_UNIT_SP,_text_size);
}
public void set70dp(View view) {
TextView t = findViewById(R.id.test2strview);
t.setTextSize(TypedValue.COMPLEX_UNIT_DIP,70);
t.setTextSize(TypedValue.COMPLEX_UNIT_DIP,_text_size);
}
public void set70px(View view) {
TextView t = findViewById(R.id.test2strview);
t.setTextSize(TypedValue.COMPLEX_UNIT_PX,70);
t.setTextSize(TypedValue.COMPLEX_UNIT_PX,_text_size);
}
public void setColor(View view) {
@ -66,7 +70,10 @@ public class MainActivity2 extends AppCompatActivity {
_g = Integer.parseInt(g_edit.getText().toString());
_b = Integer.parseInt(b_edit.getText().toString());
} catch (NumberFormatException e){
Log.e("page2.setcolor",e.toString());
NotificationHelper helper = new NotificationHelper(this);
helper.createNotificationChannel();
helper.pushNotification("Page 2: Error Setting Color",
e.toString());
return;
}
if (_r>=0&&_g>=0&&_b>=0&&_r<=255&&_g<=255&&_b<=255){
@ -79,15 +86,53 @@ public class MainActivity2 extends AppCompatActivity {
}
}
public void setText(View view) {
TextView textView = findViewById(R.id.test2strview);
EditText editText = findViewById(R.id.Page2SetTestText);
String s = "Test Text";
try {
s = editText.getText().toString();
}catch (Exception e){
NotificationHelper helper = new NotificationHelper(this);
helper.createNotificationChannel();
helper.pushNotification("Page 2: Error Setting Test Text",
e.toString());
return;
}
textView.setText(s);
}
public void goNext() {
Intent myIntent = new Intent(this, Page3.class);
startActivity(myIntent);
};
}
public void goPrev() {
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
};
}
public void setSize(View view) {
Button sp = findViewById(R.id.Page2SetSP);
Button dp = findViewById(R.id.Page2SetDP);
Button px = findViewById(R.id.Page2SetPX);
EditText ed = findViewById(R.id.P2SetTextSize);
int size = 70;
try {
size = Integer.parseInt(ed.getText().toString());
}catch (Exception e){
NotificationHelper helper = new NotificationHelper(this);
helper.createNotificationChannel();
helper.pushNotification("Page 2: Error Setting Text Size",
e.toString());
return;
}
_text_size = size;
sp.setText(size +"SP");
dp.setText(size +"DP");
px.setText(size +"PX");
}
final class PageButtonHandler implements View.OnClickListener
{
@ -105,5 +150,15 @@ public class MainActivity2 extends AppCompatActivity {
return false;
}
}
// Fix: hide keyboard
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (getCurrentFocus() != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
return super.dispatchTouchEvent(ev);
}
}

View file

@ -15,12 +15,14 @@ class NotificationHelper(_ctx: Context) {
val CHANNEL_ID = "DBG_PUSHER"
val name = "Debug Pusher"
var ctx: Context = _ctx
var notifyID = 1;
var rng = Random(114514);
companion object { // Make notify id always increase
var notifyID = 1
}
private val notificationManager =
getSystemService(ctx,NotificationManager::class.java) as NotificationManager
public fun pushNotification(title:String,content:String) {
fun pushNotification(title:String,content:String) {
val builder = NotificationCompat.Builder(ctx, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(title)
@ -39,15 +41,7 @@ class NotificationHelper(_ctx: Context) {
return
}
notify(notifyID, builder.build())
val lastNotifyID = notifyID;
while (notifyID==lastNotifyID){
notifyID = rng.nextInt()
if (notifyID<=0){
notifyID = -notifyID
notifyID += 1
}
rng = Random(notifyID)
}
notifyID += 1
}
}

View file

@ -12,22 +12,30 @@
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="35dp" />
<TextView
android:id="@+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="344dp"
android:layout_width="match_parent"
android:layout_height="620dp"
android:text="@string/hello_world"
android:textSize="100sp"
android:textSize="15sp"
android:visibility="visible" />
<Button
android:id="@+id/button2"
android:id="@+id/Page1NextPage"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/next_page"
android:onClick="jumpToNext"
android:enabled="false"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -14,52 +14,73 @@
android:orientation="vertical">
<Button
android:id="@+id/button5"
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="67dp"
android:text="Button" />
<TextView
android:id="@+id/test2strview"
android:layout_height="35dp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="292dp"
android:text="@string/test_str"
android:textSize="60sp"/>
android:layout_height="350dp">
<TextView
android:id="@+id/test2strview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/test_str"
android:textSize="60sp"/>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="153dp"
android:layout_height="100dp"
android:orientation="horizontal">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
<LinearLayout
android:layout_width="80dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="set70sp"
android:text="70sp" />
android:orientation="vertical">
<EditText
android:id="@+id/P2SetTextSize"
android:layout_width="80dp"
android:layout_height="0dp"
android:layout_weight="1"
android:ems="10"
android:inputType="number" />
<Button
android:id="@+id/Page2SetSize"
android:layout_width="80dp"
android:layout_height="5dp"
android:layout_weight="1"
android:onClick="setSize"
android:text="设置" />
</LinearLayout>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="set70dp"
android:text="70dp" />
android:id="@+id/Page2SetSP"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="set70sp"
android:text="70sp" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="set70px"
android:text="70px" />
<Button
android:id="@+id/Page2SetDP"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="set70dp"
android:text="70dp" />
<Button
android:id="@+id/Page2SetPX"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:onClick="set70px"
android:text="70px" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="83dp"
android:layout_height="70dp"
android:orientation="horizontal">
<EditText
@ -87,13 +108,37 @@
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal">
<EditText
android:id="@+id/Page2SetTestText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:ems="10"
android:inputType="text" />
<Button
android:id="@+id/Page2SetTestTextButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/setText"
android:onClick="setText"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/button6"
android:id="@+id/Page2SetColor"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"

View file

@ -2,7 +2,9 @@
<string name="app_name">Android101</string>
<string name="hello_world">你好,世界!</string>
<string name="next_page">下一页</string>
<string name="two_way_page">单击下一页,长上一页</string>
<string name="two_way_page">单击下一页,长上一页</string>
<string name="test_str">测试文本</string>
<string name="set_color">设置颜色</string>
<string name="set">设置</string>
<string name="setText">设置文本</string>
</resources>