美化,添加功能

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"> <project version="4">
<component name="deploymentTargetDropDown"> <component name="deploymentTargetDropDown">
<value> <value>
<entry key="MainActivity">
<State />
</entry>
<entry key="app"> <entry key="app">
<State /> <State />
</entry> </entry>

View file

@ -6,10 +6,12 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.view.View; import android.view.View;
import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
import android.Manifest; import android.Manifest;
import androidx.activity.EdgeToEdge; import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat; import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
@ -29,33 +31,42 @@ public class MainActivity extends AppCompatActivity {
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets; 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 @Override
protected void onResume(){ protected void onResume(){
super.onResume(); super.onResume();
}
private void goNextPage(){ TextView tv = findViewById(R.id.tv_hello);
TextView textView = findViewById(R.id.tv_hello); textViewAppendString(tv,"Starting Kagura Init");
textView.setText("3秒后进入下个页面"); if (Build.VERSION.SDK_INT >= 33) {
new Handler().postDelayed(mGoNext,3000); textViewAppendString(tv,"Find api > 33, checking permission");
} if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
textViewAppendString(tv,"Acquiring POST_NOTIFICATIONS");
private final Runnable mGoNext = new Runnable() { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 101);
@Override }
public void run() { textViewAppendString(tv,"Check Finished");
startActivity(new Intent(MainActivity.this,MainActivity2.class));
} }
}; textViewAppendString(tv,"*************************\nfinished");
Button button = findViewById(R.id.Page1NextPage);
button.setEnabled(true);
}
public void jumpToNext(View view) { 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; package uk.kagurach.android101;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Color; import android.graphics.Color;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
@ -18,6 +21,7 @@ import androidx.core.view.WindowInsetsCompat;
public class MainActivity2 extends AppCompatActivity { public class MainActivity2 extends AppCompatActivity {
int _text_size = 70;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -41,17 +45,17 @@ public class MainActivity2 extends AppCompatActivity {
public void set70sp(View view) { public void set70sp(View view) {
TextView t = findViewById(R.id.test2strview); 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) { public void set70dp(View view) {
TextView t = findViewById(R.id.test2strview); 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) { public void set70px(View view) {
TextView t = findViewById(R.id.test2strview); 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) { public void setColor(View view) {
@ -66,7 +70,10 @@ public class MainActivity2 extends AppCompatActivity {
_g = Integer.parseInt(g_edit.getText().toString()); _g = Integer.parseInt(g_edit.getText().toString());
_b = Integer.parseInt(b_edit.getText().toString()); _b = Integer.parseInt(b_edit.getText().toString());
} catch (NumberFormatException e){ } 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; return;
} }
if (_r>=0&&_g>=0&&_b>=0&&_r<=255&&_g<=255&&_b<=255){ 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() { public void goNext() {
Intent myIntent = new Intent(this, Page3.class); Intent myIntent = new Intent(this, Page3.class);
startActivity(myIntent); startActivity(myIntent);
}; }
public void goPrev() { public void goPrev() {
Intent myIntent = new Intent(this, MainActivity.class); Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent); 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 final class PageButtonHandler implements View.OnClickListener
{ {
@ -105,5 +150,15 @@ public class MainActivity2 extends AppCompatActivity {
return false; 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 CHANNEL_ID = "DBG_PUSHER"
val name = "Debug Pusher" val name = "Debug Pusher"
var ctx: Context = _ctx var ctx: Context = _ctx
var notifyID = 1; companion object { // Make notify id always increase
var rng = Random(114514); var notifyID = 1
}
private val notificationManager = private val notificationManager =
getSystemService(ctx,NotificationManager::class.java) as 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) val builder = NotificationCompat.Builder(ctx, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground) .setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(title) .setContentTitle(title)
@ -39,15 +41,7 @@ class NotificationHelper(_ctx: Context) {
return return
} }
notify(notifyID, builder.build()) notify(notifyID, builder.build())
val lastNotifyID = notifyID; notifyID += 1
while (notifyID==lastNotifyID){
notifyID = rng.nextInt()
if (notifyID<=0){
notifyID = -notifyID
notifyID += 1
}
rng = Random(notifyID)
}
} }
} }

View file

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

View file

@ -14,52 +14,73 @@
android:orientation="vertical"> android:orientation="vertical">
<Button <View
android:id="@+id/button5" android:id="@+id/view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="67dp" android:layout_height="35dp" />
android:text="Button" /> <ScrollView
<TextView
android:id="@+id/test2strview"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="292dp" android:layout_height="350dp">
android:text="@string/test_str" <TextView
android:textSize="60sp"/> android:id="@+id/test2strview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/test_str"
android:textSize="60sp"/>
</ScrollView>
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="153dp" android:layout_height="100dp"
android:orientation="horizontal"> android:orientation="horizontal">
<Button <LinearLayout
android:id="@+id/button" android:layout_width="80dp"
android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="1" android:orientation="vertical">
android:onClick="set70sp" <EditText
android:text="70sp" /> 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 <Button
android:id="@+id/button3" android:id="@+id/Page2SetSP"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:onClick="set70dp" android:onClick="set70sp"
android:text="70dp" /> android:text="70sp" />
<Button <Button
android:id="@+id/button4" android:id="@+id/Page2SetDP"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:onClick="set70px" android:onClick="set70dp"
android:text="70px" /> 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>
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="83dp" android:layout_height="70dp"
android:orientation="horizontal"> android:orientation="horizontal">
<EditText <EditText
@ -87,13 +108,37 @@
android:inputType="number" /> android:inputType="number" />
</LinearLayout> </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 <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="horizontal"> android:orientation="horizontal">
<Button <Button
android:id="@+id/button6" android:id="@+id/Page2SetColor"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"

View file

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